Question

I have a TreeView that I fill with data from a XmlDataProvider. The is something I have created my self. And I want to sort the items in the TreeView but can't get it to work. So I have made you an sample code that you can just paste into your VS and test, and maybe you can see whats wrong with the code.

XAML

<Window x:Class="TreeViewSortingWithXmlDataProvider.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <HierarchicalDataTemplate DataType="Header" ItemsSource="{Binding XPath=Header|Group|Item}">
            <TextBlock Text="{Binding XPath=@Name}" Margin="3,0,0,0" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Header|Group|Item}">
            <TextBlock Text="{Binding XPath=@Name, Mode=TwoWay}" Margin="3,0,0,0" />
        </HierarchicalDataTemplate>

        <DataTemplate DataType="Item">
            <TextBlock Text="{Binding XPath=@Name}" />
        </DataTemplate>

    </Window.Resources>
    <DockPanel >
        <Button DockPanel.Dock="Bottom" Content="click" Click="Button_Click" />
        <TreeView Name="myTreeView"/>
    </DockPanel>
</Window>

And CodeBehind

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml.Linq;
using System.Xml;
using System.ComponentModel;

namespace TreeViewSortingWithXmlDataProvider
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            XmlDocument xmlDoc = new XmlDocument();
            XDocument xDoc = new XDocument(
                                    new XDeclaration("1.0", "utf-8", "yes"),
                                    new XComment("Test of sorting"),
                                    new XElement("Header", new XAttribute("Name", "Header")));

            XElement groupElementA = new XElement("Group", new XAttribute("Name", "A"));
            XElement groupElementB = new XElement("Group", new XAttribute("Name", "B"));

            XElement item1 = new XElement("Item", new XAttribute("Name", "TestItem1"), new XAttribute("sortPos", "0"));
            XElement item2 = new XElement("Item", new XAttribute("Name", "TestItem0"), new XAttribute("sortPos", "2"));
            XElement item3 = new XElement("Item", new XAttribute("Name", "TestItem3"), new XAttribute("sortPos", "1"));

            XElement item4 = new XElement("Item", new XAttribute("Name", "TestItem4"), new XAttribute("sortPos", "0"));
            XElement item5 = new XElement("Item", new XAttribute("Name", "TestItem5"), new XAttribute("sortPos", "2"));

            groupElementA.Add(item1);
            groupElementA.Add(item2);
            groupElementA.Add(item3);

            groupElementB.Add(item4);
            groupElementB.Add(item5);

            xDoc.Element("Header").Add(groupElementA);
            xDoc.Element("Header").Add(groupElementB);

            xmlDoc.LoadXml(xDoc.ToString());
            XmlDataProvider provider = new XmlDataProvider() { Document = xmlDoc, XPath = "Header" };
            this.myTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding() { Source = provider });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.myTreeView.Items.SortDescriptions.Clear();
            this.myTreeView.Items.SortDescriptions.Add(new SortDescription("@sortPos", ListSortDirection.Ascending));
        }
    }
}

So what I want to happen is that when I press the Button the sorting of the TreeView should change and be sorted after the sortPos instead of just the same order as I added them to the xml.

Was it helpful?

Solution

OK, so thanks to @Maverik I managed to do the following to solve this (I'll paste the whole code again...)

XAML

<Window x:Class="TreeViewSortingWithXmlDataProvider.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:l="clr-namespace:TreeViewSortingWithXmlDataProvider"
        Title="MainWindow" Height="350" Width="525">
    <Window.Resources>
        <l:MyConverter x:Key="myConverter" />
        
        <HierarchicalDataTemplate DataType="Header" ItemsSource="{Binding XPath=Header|Group|Item}">
            <TextBlock Text="{Binding XPath=@Name}" Margin="3,0,0,0" />
        </HierarchicalDataTemplate>

        <HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Item, Converter={StaticResource myConverter}, ConverterParameter=sortPos}">
            <TextBlock Text="{Binding XPath=@Name, Mode=TwoWay}" Margin="3,0,0,0" />
            <HierarchicalDataTemplate.ItemTemplate>
                <DataTemplate>
                    <TextBlock Text="{Binding XPath=@Name}" />
                </DataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </Window.Resources>
    <DockPanel >
        <Button DockPanel.Dock="Bottom" Content="click" Click="Button_Click" />
        <TreeView Name="myTreeView"/>
    </DockPanel>
</Window>

And the code-behind (with the Converter)

using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Xml.Linq;
using System.Xml;
using System.ComponentModel;
using System.Globalization;

namespace TreeViewSortingWithXmlDataProvider
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();

            XmlDocument xmlDoc = new XmlDocument();
            XDocument xDoc = new XDocument(
                                    new XDeclaration("1.0", "utf-8", "yes"),
                                    new XComment("Test of sorting"),
                                    new XElement("Header", new XAttribute("Name", "Header")));

            XElement groupElementA = new XElement("Group", new XAttribute("Name", "A"));
            XElement groupElementB = new XElement("Group", new XAttribute("Name", "B"));

            XElement item1 = new XElement("Item", new XAttribute("Name", "TestItem1"), new XAttribute("sortPos", "3"));
            XElement item2 = new XElement("Item", new XAttribute("Name", "TestItem0"), new XAttribute("sortPos", "2"));
            XElement item3 = new XElement("Item", new XAttribute("Name", "TestItem3"), new XAttribute("sortPos", "1"));

            XElement item4 = new XElement("Item", new XAttribute("Name", "TestItem4"), new XAttribute("sortPos", "0"));
            XElement item5 = new XElement("Item", new XAttribute("Name", "TestItem5"), new XAttribute("sortPos", "2"));

            groupElementA.Add(item1);
            groupElementA.Add(item2);
            groupElementA.Add(item3);

            groupElementB.Add(item4);
            groupElementB.Add(item5);

            xDoc.Element("Header").Add(groupElementA);
            xDoc.Element("Header").Add(groupElementB);

            xmlDoc.LoadXml(xDoc.ToString());
            XmlDataProvider provider = new XmlDataProvider() { Document = xmlDoc, XPath = "Header" };
            this.myTreeView.SetBinding(TreeView.ItemsSourceProperty, new Binding() { Source = provider });
        }

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            this.myTreeView.Items.SortDescriptions.Clear();
            this.myTreeView.Items.SortDescriptions.Add(new SortDescription("@Name", ListSortDirection.Descending));
        }
    }

    public class MyConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            System.Collections.IList collection = value as System.Collections.IList;
            ListCollectionView view = new ListCollectionView(collection);
            SortDescription sort = new SortDescription(parameter.ToString(), ListSortDirection.Ascending);
            view.SortDescriptions.Add(sort);

            return view;
        }

        public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
        {
            return null;
        }
    }
}

(Sorry for the rather long example, but was easier then to explain every changes I made)

If you have any other solution please add it!

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top