1

我有一个 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));
        }
    }
}

所以我想要发生的是,当我按下按钮时,TreeView 的排序应该改变并在之后排序,sortPos而不是与我将它们添加到 xml 的顺序相同。

4

1 回答 1

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

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

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

于 2013-04-10T07:32:25.277 回答