title: WPF DataBound treeview expand / collapse
body: <p>I'm just trying to find a way to control the expand / collapse of the <code>TreeView</code> nodes through the object they're bound to. The object has an <code>IsExpanded</code> property, and I want to use that to show the <code>TreeView</code> node itself expanded or collapsed based on that property.</p>
  
  <p>Here's my code:</p>
  
  <p>C#:</p>
  
  <pre><code>public partial class Window2 : Window
  {
      public Window2()
      {
          InitializeComponent();
  
          this.DataContext = new List&lt;Parent&gt;() { Base.GetParent("Parent 1"), Base.GetParent("Parent 2") };
      }
  }
  
  public class Base
  {
      public string Name { get; set; }
      public bool IsExpanded { get; set; }
  
      public static Parent GetParent(string name)
      {
          Parent p = new Parent() { Name = name };
  
          p.Children.Add(new Child() { Name = "Child 1", GrandChildren = new ObservableCollection&lt;GrandChild&gt;() { new GrandChild() { Name = "Grandchild 1" } } });
          p.Children.Add(new Child() { Name = "Child 2", GrandChildren = new ObservableCollection&lt;GrandChild&gt;() { new GrandChild() { Name = "Grandchild 1" } } });
          p.Children.Add(new Child() { Name = "Child 3", GrandChildren = new ObservableCollection&lt;GrandChild&gt;() { new GrandChild() { Name = "Grandchild 1" } } });
  
          return p;
      }
  }
  
  public class Parent : Base
  {
      public ObservableCollection&lt;Child&gt; Children { get; set; }
  
      public Parent()
      {
          this.Children = new ObservableCollection&lt;Child&gt;();
      }
  }
  
  public class Child : Base
  {
      public ObservableCollection&lt;GrandChild&gt; GrandChildren { get; set; }
  
      public Child()
      {
          this.GrandChildren = new ObservableCollection&lt;GrandChild&gt;();
      }
  }
  
  public class GrandChild : Base
  {
  }
  </code></pre>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="HeterogeneousExperimentExplorer.Window2"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          xmlns:local="clr-namespace:HeterogeneousTree"
      Title="Window2" Height="300" Width="300"&gt;
      &lt;Window.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding Children}"&gt;
              &lt;TextBlock Text="{Binding Name}" /&gt;
              &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                  &lt;HierarchicalDataTemplate DataType="{x:Type local:Parent}" ItemsSource="{Binding GrandChildren}"&gt;
                      &lt;TextBlock Text="{Binding Name}" /&gt;
                      &lt;HierarchicalDataTemplate.ItemTemplate&gt;
                          &lt;DataTemplate&gt;
                              &lt;TextBlock Text="{Binding Name}" /&gt;
                          &lt;/DataTemplate&gt;
                      &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/HierarchicalDataTemplate.ItemTemplate&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;TreeView ItemsSource="{Binding}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>