在TreeView中对XmlData进行排序

时间:2013-04-04 13:23:57

标签: c# wpf treeview xmldataprovider

我有一个TreeView,我用XmlDataProvider填充数据。这是我创造自我的东西。 我想在TreeView中对项目进行排序,但无法使其工作。 所以我给你做了一个示例代码,你可以将其粘贴到你的VS中进行测试,也许你可以看到代码中的错误。

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>

和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));
        }
    }
}

所以我想要发生的是当我按下Button时,TreeView的排序应该改变并在sortPos之后排序,而不是像我将它们添加到xml中那样排序。

1 个答案:

答案 0 :(得分:0)

好的,感谢@Maverik我设法做了以下事情来解决这个问题(我将再次粘贴整个代码......)

<强> 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>

代码隐藏(使用转换器)

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;
        }
    }
}

(抱歉这个相当长的例子,但更容易解释我所做的每一项改变)

如果您有任何其他解决方案,请添加它!