title: DataTemplate inside HierarchicalDataTemplate
body: <p>I needed to build a custom treeview as a user control. I called it for the sake of the example TreeViewEx :</p>
  
  <pre><code>&lt;UserControl x:Class="WpfApplication4.TreeViewEx"
               xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
               xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
               x:Name="root"&gt;
      &lt;Grid&gt;
          &lt;TreeView ItemsSource="{Binding Path=ItemsSource, ElementName=root}"&gt;
              &lt;TreeView.ItemTemplate&gt;
                  &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt;
                      &lt;StackPanel Orientation="Horizontal"&gt;
                          &lt;TextBlock Text="Node : "/&gt;
                          &lt;ContentControl Content="{Binding Path=AdditionalContent, ElementName=root}"/&gt;
                      &lt;/StackPanel&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/TreeView.ItemTemplate&gt;
          &lt;/TreeView&gt;
      &lt;/Grid&gt;
  &lt;/UserControl&gt;
  </code></pre>
  
  <p>The idea is to have a fixed part of the content of the ItemTemplate and a customizable part of it.</p>
  
  <p>Of course, I created two dependency properties on the TreeViewEx class :</p>
  
  <pre><code>public partial class TreeViewEx
  {
      public static readonly DependencyProperty ItemsSourceProperty = DependencyProperty.Register(
          "ItemsSource", typeof(IEnumerable), typeof(TreeViewEx));
  
      public IEnumerable ItemsSource
      {
          get { return (IEnumerable)GetValue(ItemsSourceProperty); }
          set { SetValue(ItemsSourceProperty, value); }
      }
  
      public static readonly DependencyProperty AdditionalContentProperty = DependencyProperty.Register(
          "AdditionalContent", typeof(object), typeof(TreeViewEx));
  
      public object AdditionalContent
      {
          get { return GetValue(AdditionalContentProperty); }
          set { SetValue(AdditionalContentProperty, value); }
      }
  
      public TreeViewEx()
      {
          InitializeComponent();
      }
  }
  </code></pre>
  
  <p>Having a simple node class like so :</p>
  
  <pre><code>public class Node
  {
      public string Name { get; set; }
      public int Size { get; set; }
      public IEnumerable&lt;Node&gt; Children { get; set; }
  }
  </code></pre>
  
  <p>I would feed the treeview. I place an instance of TreeViewEx on the MainWindow of a WPF test project :</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication4.MainWindow"
          xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
          xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
          Title="MainWindow" Height="350" Width="525"
          xmlns:local="clr-namespace:WpfApplication4"&gt;
      &lt;Grid&gt;
          &lt;local:TreeViewEx x:Name="tree"&gt;
              &lt;local:TreeViewEx.AdditionalContent&gt;
                  &lt;TextBlock Text="{Binding Name}"/&gt;
              &lt;/local:TreeViewEx.AdditionalContent&gt;
          &lt;/local:TreeViewEx&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>And finally feed it :</p>
  
  <pre><code>public partial class MainWindow
  {
      public MainWindow()
      {
          InitializeComponent();
  
          var dummyData = new ObservableCollection&lt;Node&gt;
          {
              new Node
              {
                  Name = "Root", 
                  Size = 3,
                  Children = new ObservableCollection&lt;Node&gt;
                  {
                      new Node{
                          Name="Child1",
                          Size=2,
                          Children = new ObservableCollection&lt;Node&gt;{
                              new Node{
                                  Name = "Subchild", 
                                  Size = 1
                              }
  
                          }
                      }
  
                  }
              }
          };
  
          tree.ItemsSource = dummyData;
      }
  }
  </code></pre>
  
  <p>However it doesn't work as expected namely at first the ContentControl has the data but as I expand the nodes it does not display the ContentControl's content.</p>
  
  <p>I don't really get it.. I should use a <strong>DataTemplate</strong> or something else?</p>