title: WPF Treeview - Binding to a collection with different depths, and styling differently
body: <p>Apologies for the long post -- Read a few threads about this, but still find it hard to implement. Basically, I have a collection of objects, defined as:</p>
  
  <pre><code>public class LibData
  {
      public string Name { get; set; }
      ObservableCollection&lt;LibObject&gt; _list;
      public ObservableCollection&lt;LibObject&gt; List { get { return _list; } }
  
      public LibData(string name, LibDataType type)
      {
          this.Name = name;
          _list = new ObservableCollection&lt;LibObject&gt;();
      }
  }
  </code></pre>
  
  <p>and the object:</p>
  
  <pre><code>public class LibObject
  {
      public string Name { get; set; }
  
      public LibObject(string name)
      {
          this.Name = name;
      }
  }
  </code></pre>
  
  <p>My main problem is in the XAML, and styling this TreeView. I need to have a specific style for a "root" item, and a specific style for a "leaf". Thing is, that one item in the bound list is "Root->Leaf", and another is "Root->Child->Leaf".
  I tried this:</p>
  
  <pre><code>&lt;TreeView x:Name="myTree" ItemsSource="{x:Static local:myDataList}"&gt;
       &lt;TreeView.ItemTemplate&gt;
           &lt;HierarchicalDataTemplate ItemsSource="{Binding Path=List}" &gt;
               &lt;Grid&gt;
                   &lt;StackPanel Orientation="Horizontal"&gt;
                        &lt;TextBlock Text="{Binding Path=Name}" /&gt;
                        &lt;CheckBox IsChecked="True" Content="HeaderCheckbox"/&gt;
                   &lt;/StackPanel&gt;
               &lt;/Grid&gt;
           &lt;HierarchicalDataTemplate.ItemTemplate &gt;
                &lt;DataTemplate&gt;
                    &lt;Grid&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;CheckBox IsChecked="True" Content="LeafCheckbox" /&gt;
                  &lt;TextBlock Text="{Binding Path=Name}"/&gt;
              &lt;/StackPanel&gt;
                    &lt;/Grid&gt;
                &lt;/DataTemplate&gt;
            &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
        &lt;/HierarchicalDataTemplate&gt;
    &lt;/TreeView.ItemTemplate&gt;
  </code></pre>
  
  <p></p>
  
  <p>This obviously works fine for the "Root->Leaf" item, but not for the "Root-Child-Leaf".
  The XAML implementation seems to be more of a "hard coded" solution, where I know that the item layout is always "Root->Leaf" - how do I make this dynamic?
  Again, I have seen solutions to different levels (including using converters), but the problem i'm having is that I need specific styles to root and leaf and nothing for levels in between.
  I'm wondering if I'm looking at this completely wrong ...</p>