views:

261

answers:

1

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>
A: 

Try adding another attribute to your XML file, I'll call it FileType, but you can call it whatever your like. For this element specify it to be equal to "Folder" or "File". Now you must do to levels of sorting. First sort Decending on FileType (Folders first, Files second), then sort on the Name attribute. In other words, you XML would like like this:

<project name="ProjectName" >
    <file name="alphacat.html" FileType="File" />
    <file name="aztec.html" FileType="File" />
    <folder name="FolderA" FileType="Folder" >
        <file name="application.asp" FileType="File" />
        <file name="work.asp" FileType="File" /> 
    </folder> 
</project>

Does that help?

Brent
Thanks for the reply. I understand what the sort criteria should be but I don't know how to create the sorts in the xaml. I wil add the attribute like you suggested (I was hoping to sort on the element name, but don't really need to). Can you show me how to code the xaml?
danhotb
Hmm... that's strange. It works fine when I use a ListView bound to a CollectionViewSource with SortDescriptors, but a TreeView doesn't seem to pick up the sort. Sorry about that. One workaround would be to order your XML the way you want it displayed - Folders first, files second. It's not ideal, but it would work. I can delete my answer if you think you might help you get more answers from others. Let me know.
Brent