我有一个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>
和后台代码
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));
}
}
}
所以我想要发生的是,当我按下按钮的排序树视图应该改变和排序后的sortPos
,而不是只是相同的顺序,因为我把它们添加到xml
好的,所以感谢@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>
和后面的代码(带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;
}
}
}
(很抱歉,这个例子很长,但这样更容易解释我所做的每一个更改)
如果您有任何其他解决方案请添加!