我有一个 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 的顺序相同。