title: ControlTemplate for second level of TreeView
body: <p>I'm trying to create a <code>TreeView</code> which has two levels but I'm getting nowhere. I have an <code>ItemTemplateSelector</code> (which works - selecting either of the first two templates listed below), but I can't get the children to populate using a <code>DataTemplate</code>. This is my code:</p>
  
  <pre><code>public class PlannedMealGroup : INotifyPropertyChanged {
          public ObservableCollection&lt;PlannedMeal&gt; Meals;
          public Constants.MealTimes MealTime;
    ...// these implement INotifyPropertyChanged
  }
  
  &lt;HierarchicalDataTemplate x:Key="groupTemplate" ItemsSource="{Binding Meals}"&gt;
      &lt;TextBlock Text="{Binding Path=MealTime}"/&gt;
      &lt;HierarchicalDataTemplate.ItemContainerStyle&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}"&gt;
              &lt;EventSetter Event="TreeViewItem.PreviewMouseRightButtonDown" Handler="tre_PreviewMouseRightButtonDown"/&gt;
          &lt;/Style&gt;
      &lt;/HierarchicalDataTemplate.ItemContainerStyle&gt;
  &lt;/HierarchicalDataTemplate&gt;
  &lt;DataTemplate x:Key="mealTemplate"&gt;
      &lt;Border Name="Border" Margin="4,2" Padding="3" Background="Transparent" IsHitTestVisible="True" SnapsToDevicePixels="true" BorderThickness="0.6" CornerRadius="3"&gt;
          &lt;Border Name="InnerBorder" Background="Transparent" IsHitTestVisible="True"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;Image Source="{Binding Recipe.Icon,Converter={StaticResource IconConverter}, ConverterParameter=16}" Stretch="None" /&gt;
                  &lt;TextBlock Text="{Binding Converter={StaticResource RecipeServingConverter}}"/&gt;
              &lt;/StackPanel&gt;
          &lt;/Border&gt;
      &lt;/Border&gt;
  
  &lt;TreeView ItemTemplateSelector="{StaticResource PlannedMealTemplateSelector}" Style="{StaticResource GroupedTreeView}"&gt;
    &lt;TreeView.ItemsPanel&gt;
        &lt;ItemsPanelTemplate&gt;
            &lt;StackPanel IsItemsHost="True" Orientation="{Binding Orientation,RelativeSource={x:Static RelativeSource.TemplatedParent}}" /&gt;
        &lt;/ItemsPanelTemplate&gt;
    &lt;/TreeView.ItemsPanel&gt;
    &lt;TreeView.ContextMenu&gt;
    ...
  
  &lt;Style x:Key="GroupedTreeViewItem" TargetType="{x:Type TreeViewItem}"&gt;
    &lt;Setter Property="IsExpanded" Value="True"/&gt;
    &lt;Setter Property="Template"&gt;
    &lt;Setter.Value&gt;
      &lt;ControlTemplate TargetType="{x:Type TreeViewItem}"&gt;
      &lt;Border Name="Border" Margin="4,2" Padding="3" Background="{StaticResource GroupBackgroundBrush}" IsHitTestVisible="True" SnapsToDevicePixels="true" BorderThickness="0.6" CornerRadius="3"&gt;
              &lt;Expander Name="Exp" IsExpanded="{Binding IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}},Mode=TwoWay}"&gt;
                  &lt;Expander.Header&gt;
                      &lt;ContentPresenter SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}" HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}" 
                          x:Name="PART_Header" ContentSource="Header"/&gt;
                  &lt;/Expander.Header&gt;
                  &lt;ItemsPresenter x:Name="ItemsHost"/&gt;
              &lt;/Expander&gt;
          &lt;/Border&gt;
      &lt;/ControlTemplate&gt;
    &lt;/Setter.Value&gt;
    &lt;/Setter&gt;
  &lt;/Style&gt;
  &lt;Style x:Key="GroupedTreeView" TargetType="TreeView"&gt;
      &lt;Setter Property="ItemContainerStyle" Value="{StaticResource GroupedTreeViewItem}"/&gt;
  &lt;/Style&gt;
  </code></pre>
  
  <p>Thanks for any pointers.</p>