Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>OK, so thanks to <a href="http://chat.stackoverflow.com/rooms/18165/wpf">@Maverik</a> I managed to do the following to solve this (I'll paste the whole code again...)</p> <p><strong>XAML</strong></p> <pre><code>&lt;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"&gt; &lt;Window.Resources&gt; &lt;l:MyConverter x:Key="myConverter" /&gt; &lt;HierarchicalDataTemplate DataType="Header" ItemsSource="{Binding XPath=Header|Group|Item}"&gt; &lt;TextBlock Text="{Binding XPath=@Name}" Margin="3,0,0,0" /&gt; &lt;/HierarchicalDataTemplate&gt; &lt;HierarchicalDataTemplate DataType="Group" ItemsSource="{Binding XPath=Item, Converter={StaticResource myConverter}, ConverterParameter=sortPos}"&gt; &lt;TextBlock Text="{Binding XPath=@Name, Mode=TwoWay}" Margin="3,0,0,0" /&gt; &lt;HierarchicalDataTemplate.ItemTemplate&gt; &lt;DataTemplate&gt; &lt;TextBlock Text="{Binding XPath=@Name}" /&gt; &lt;/DataTemplate&gt; &lt;/HierarchicalDataTemplate.ItemTemplate&gt; &lt;/HierarchicalDataTemplate&gt; &lt;/Window.Resources&gt; &lt;DockPanel &gt; &lt;Button DockPanel.Dock="Bottom" Content="click" Click="Button_Click" /&gt; &lt;TreeView Name="myTreeView"/&gt; &lt;/DockPanel&gt; &lt;/Window&gt; </code></pre> <p><strong>And the code-behind</strong> (with the Converter)</p> <pre><code>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 { /// &lt;summary&gt; /// Interaction logic for MainWindow.xaml /// &lt;/summary&gt; 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; } } } </code></pre> <p>(Sorry for the rather long example, but was easier then to explain every changes I made)</p> <p>If you have any other solution please add it!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload