title: How do I sort a WPF treeview that has items bound to the properties of an Item subclass?
body: <p>I have two classes, </p>
  
  <pre><code>public class BookItem
  {
      public string BookID            { get; set; }
      public string ItemID            { get; set; }
      public Item Item                { get; set; }   
      public ItemType Type            { get; set; }
      public string ParentID          { get; set; }
      public string BoxID             { get; set; }
      public string StyleID           { get; set; }
      public string NotesID           { get; set; }
      public string Code_XAML         { get; set; }
      public string Description_XAML      { get; set; }
      public CompositeCollection SubItems { get; set; }
  }
  
  public class Item : ClaunchBaseClass
  {
      public string ItemID        { get; set; }
      public int    Type          { get; set; }
      public string Code          { get; set; }
      public string Description   { get; set; }
      private BookList _books = new BookList();
      public BookList Books       { get {return _books;} set { _books = value; }}
  }
  </code></pre>
  
  <p>and I've created the following XAML:</p>
  
  <pre><code>&lt;pre&gt;    
  &lt;TreeView Name="tvList" Grid.Row="2" MouseDoubleClick="tvList_MouseDoubleClick"&gt;
      &lt;TreeView.ItemTemplate&gt;
          &lt;HierarchicalDataTemplate DataType="x:Type j:BookItem" ItemsSource="{Binding SubMenu}"&gt;
              &lt;Grid&gt;
                  &lt;Grid.ColumnDefinitions&gt;
                      &lt;ColumnDefinition Width="50"/&gt;
                      &lt;ColumnDefinition Width="*"/&gt;
                  &lt;/Grid.ColumnDefinitions&gt;
                  &lt;TextBlock Text="{Binding Item.Code}" Grid.Column="0" /&gt;
                  &lt;TextBlock Text="{Binding Item.Description}" Grid.Column="1"/&gt;
              &lt;/Grid&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/TreeView.ItemTemplate&gt;
  &lt;/TreeView&gt;
  &lt;code&gt;
  </code></pre>
  
  <p>This XAML binds the treeview items to the collection of book items and displays the Item subclass's Description and Code, the treeview populates and displays correctly but now I want to sort the treeview on either Item.Code or Item.Description and have tried the following with no results:</p>
  
  <pre><code>&lt;pre&gt; 
  var bookItemsSort = CollectionViewSource.GetDefaultView(_bookItemList) as ListCollectionView;
  tvList.ItemsSource = _bookItemList;         //bind the book items to the treeview
  bookItemsSort.SortDescriptions.Clear();
  bookItemsSort.SortDescriptions.Add(new SortDescription(sort, Ascending));
  &lt;code&gt;
  </code></pre>
  
  <p>I've had this code work correctly for other treeviews so I can only guess it is a problem with binding to a subclass.</p>