title: WPF TreeView hierarchical data binding to object model
body: <p>C# / WPF. I am trying to display my object model in a TreeView, with data bound using <code>&lt;HierarchicalDataTemplate&gt;</code>. I am basing my method on <a href="http://www.blackwasp.co.uk/WPFTreeViewDataBinding.aspx" rel="nofollow noreferrer">this description</a>.</p>
  
  <p>I have created an object hierarchy for testing purposes. The code runs, but the tree view only displays the top-level item (in this case the house address). I have not been able to get it to display any sub nodes. I understand that the top-level object has to be an ObservableCollection so I create one for the purposes of the TreeView.</p>
  
  <p>What am I doing wrong here?</p>
  
  <pre><code>public class House {
      public string Address { get; set; } = "Baker Street";
      public List&lt;Dog&gt; dog = new List&lt;Dog&gt;() { new Dog() };
  }
  public class Dog {
      public string Name { get; set; } = "Rover";
      public List&lt;Claw&gt; claw = new List&lt;Claw&gt;() { new Claw() };
      public List&lt;Whisker&gt; whisker = new List&lt;Whisker&gt;() { new Whisker() };
      public Tail tail = new Tail();
      public Dog() { }
  }
  public class Claw {
      public float length { get; set; } = 0;
      public Claw() { }
  }
  public class Whisker {
      public float length { get; set; } = 0;
      public Whisker() { }
  }
  public class Tail
  {
      public float length { get; set; } = 0;
      public Tail() { }
  }
  </code></pre>
  
  <hr>
  
  <pre><code>ObservableCollection&lt;House&gt; _house = new ObservableCollection&lt;House&gt;(){ new House() };
  treeViewConfig2.ItemsSource = _house;
  </code></pre>
  
  <hr>
  
  <pre><code>&lt;StackPanel&gt;
      &lt;StackPanel.Resources&gt;
  
          &lt;DataTemplate x:Key="TailTemplate"&gt;
              &lt;TextBlock Text="{Binding Length}" Foreground="Green" /&gt;
          &lt;/DataTemplate&gt;
  
          &lt;HierarchicalDataTemplate x:Key="DogTemplate"
              ItemsSource="{Binding Tail}"
              ItemTemplate="{StaticResource TailTemplate}"&gt;
              &lt;TextBlock Text="{Binding Name}" Foreground="Red" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
  
          &lt;HierarchicalDataTemplate x:Key="HouseTemplate"
              ItemsSource="{Binding Dog}"
              ItemTemplate="{StaticResource DogTemplate}"&gt;
              &lt;TextBlock Text="{Binding Address}" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
  
      &lt;/StackPanel.Resources&gt;
      &lt;TreeView Name ="treeViewConfig2" ItemsSource="{Binding}" ItemTemplate="{StaticResource HouseTemplate}"/&gt;
  
  &lt;/StackPanel&gt;
  </code></pre>