title: WPF Treeview Databinding Hierarchal Data with mixed types
body: <p>I have a bit of a complex situation with WPF Treeview Binding.  I have spent the last 2 days trying Google it, and <a href="https://stackoverflow.com/questions/1214967/how-to-use-the-wpf-treeview-with-nested-objects-of-different-types" title="Similar Question">this</a> is the closed I came up with, but it doesn't solve the issue.</p>
  <p>Here is the situation:</p>
  <p>I have an object that looks like this:</p>
  <pre><code>public class Category
  {
    public string Name { get; set; }
    public List&lt;Category&gt; Categories { get; set; }
    public List&lt;Product&gt; Products { get; set; }
  }
  
  public class Product
  {
    public string Name { get; set;
  }
  </code></pre>
  <p>Each Category can have a list of objects, and child categories.  I have a reason for doing this that makes complete sense to me, and the app I am writing.</p>
  <p>The actual object construction may look Something like this:</p>
  <pre><code>Category - Pharmacy
    |-Product - Aspirin
    |-Product - Tylenol
    |-Category - Tooth Paste
    |  |-Product - Crest
    |  |-Product - Colgate
    |-Category - Paper Products
     |-Category - Toilet Paper
     |  |-Product - NoName
     |  |-Product - Charmin
     |-Category - Facial Tissue
        |-Product - Kleenex
  Category - Household
    |-Product - Pinesol Cleaner
    |-Product - Garbage Bags
  </code></pre>
  <p>Now, I am trying to databind this relationship to a treeview. I would like the TreeView to look almost identical to the above object construct.</p>
  <p>So far I have my XAML Treeview look like this:</p>
  <pre><code>  &lt;TreeView x:Name=&quot;CategoryList&quot; Margin=&quot;8&quot; Grid.Row=&quot;2&quot; Grid.RowSpan=&quot;2&quot; ItemsSource=&quot;{Binding Path=Categories}&quot;&gt;
              &lt;TreeView.Resources&gt;
                  &lt;HierarchicalDataTemplate DataType=&quot;{x:Type src:Category}&quot; ItemsSource=&quot;{Binding Products}&quot;&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock Text=&quot;{Binding Path=Name}&quot; /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/HierarchicalDataTemplate&gt;
                  &lt;HierarchicalDataTemplate DataType=&quot;{x:Type src:Product}&quot;&gt;
                      &lt;StackPanel&gt;
                          &lt;TextBlock Text=&quot;{Binding Path=Name}&quot; /&gt;
                      &lt;/StackPanel&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/TreeView.Resources&gt;
          &lt;/TreeView&gt;
  </code></pre>
  <p>This works great for the main list of Categories, and each of it's sub products.  But it doesn't go deeper and display the sub categories under each category.</p>
  <p>Is there any way to do this directly with Templates so that each item (category or product) is selected?  I am using an MVVM pattern and don't want to resort to using the code behind, but will if it is necessary.</p>