help with grouping and sorting for TreeView in xaml
Posted
by danhotb
on Stack Overflow
See other posts from Stack Overflow
or by danhotb
Published on 2010-01-24T15:36:08Z
Indexed on
2010/03/18
9:01 UTC
Read the original article
Hit count: 559
I am having problems getting my head around grouping and sorting in xaml and hope someone can get me straightened out!
I have creaed an xml file from a tree of files and folders (just like windows explorer) that can be serveral levels deep. I have bound a TreeView control to an xml datasource and it works great! It sorts everything alphabetically but ... I would like it to sort all folders first then all files, rather than folders listed with files, as it does now.
the xml :
if you load this to a treeviw it will display the two files before the folder because they are first in alpha-order.
here is my code:
<!-- This will contain the XML-data. -->
<XmlDataProvider x:Key="xmlDP" XPath="*">
<x:XData>
<Select_Project />
</x:XData>
</XmlDataProvider>
<!-- This HierarchicalDataTemplate will visualize all XML-nodes -->
<HierarchicalDataTemplate DataType="project" ItemsSource ="{Binding}">
<TextBlock Text="{Binding XPath=@name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="folder" ItemsSource ="{Binding}">
<TextBlock Text="{Binding XPath=@name}" />
</HierarchicalDataTemplate>
<HierarchicalDataTemplate DataType="file" ItemsSource ="{Binding}">
<TextBlock Text="{Binding XPath=@name}" />
</HierarchicalDataTemplate>
<CollectionViewSource x:Key="projectView" Source="{StaticResource xmlDP}">
<CollectionViewSource.SortDescriptions>
<!-- ADD SORT DESCRIPTION HERE -->
</CollectionViewSource.SortDescriptions>
</CollectionViewSource>
<TreeView Margin="11,79.992,18,19.089"
Name="tvProject"
BorderThickness="1" FontSize="12" FontFamily="Verdana">
<TreeViewItem ItemsSource="{Binding Source={StaticResource xmlDP}, XPath=*}"
Header="Project"/>
</TreeView>
© Stack Overflow or respective owner