title: Grouping and sorting for TreeView in xaml
body: <p>I am having problems getting my head around grouping and sorting in xaml and hope someone can get me straightened out!</p>
  
  <p>I have created an xml file from a tree of files and folders (just like windows explorer) that can be several 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.</p>
  
  <p>the xml :
  
      
      
      
        
        
     
  </p>
  
  <p>if you load this to a treeviw it will display the two files before the folder because they are first in alpha-order.</p>
  
  <p>here is my code:</p>
  
  <pre><code>  &lt;!-- This will contain the XML-data. --&gt;
    &lt;XmlDataProvider x:Key="xmlDP" XPath="*"&gt;
       &lt;x:XData&gt;
          &lt;Select_Project /&gt;
       &lt;/x:XData&gt;
    &lt;/XmlDataProvider&gt;
  
    &lt;!-- This HierarchicalDataTemplate will visualize all XML-nodes --&gt;
    &lt;HierarchicalDataTemplate DataType="project" ItemsSource ="{Binding}"&gt;
       &lt;TextBlock Text="{Binding XPath=@name}" /&gt;
    &lt;/HierarchicalDataTemplate&gt;
  
    &lt;HierarchicalDataTemplate DataType="folder" ItemsSource ="{Binding}"&gt;
       &lt;TextBlock Text="{Binding XPath=@name}" /&gt;
    &lt;/HierarchicalDataTemplate&gt;
  
    &lt;HierarchicalDataTemplate DataType="file" ItemsSource ="{Binding}"&gt;
       &lt;TextBlock Text="{Binding XPath=@name}" /&gt;
    &lt;/HierarchicalDataTemplate&gt;
  
    &lt;CollectionViewSource x:Key="projectView" Source="{StaticResource xmlDP}"&gt;
       &lt;CollectionViewSource.SortDescriptions&gt;
          &lt;!-- ADD SORT DESCRIPTION HERE --&gt;
       &lt;/CollectionViewSource.SortDescriptions&gt;
    &lt;/CollectionViewSource&gt;
  
    &lt;TreeView Margin="11,79.992,18,19.089" 
              Name="tvProject" 
              BorderThickness="1" FontSize="12" FontFamily="Verdana"&gt;
  
       &lt;TreeViewItem ItemsSource="{Binding Source={StaticResource xmlDP}, XPath=*}"  
                     Header="Project"/&gt;
    &lt;/TreeView&gt;
  </code></pre>