title: Changing the template of a TreeViewItem when it is selected
body: <p>I'm having some problems changing the DataTemplate that is used for a TreeViewItem when it is selected. Ideally, I would like each item to contain a <code>TextBlock</code>, and then when selected it should contain a <code>TextBox</code> instead.</p>
  
  <p>Here is what I have so far (I used <a href="https://stackoverflow.com/questions/146269/change-wpf-datatemplate-for-listbox-item-if-selected">this question</a> as a starting point):</p>
  
  <pre><code>&lt;Window&gt;
      &lt;Window.Resources&gt;
          &lt;HierarchicalDataTemplate x:Key="normal"
              ItemsSource="{Binding Path=Children}"&gt;
              &lt;TextBlock Text="{Binding Path=Text}" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
          &lt;HierarchicalDataTemplate x:Key="selected"
              ItemsSource="{Binding Path=Children}"&gt;
              &lt;TextBox Text="{Binding Path=Text}" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
          &lt;Style TargetType="{x:Type TreeViewItem}" x:Key="ContainerStyle"&gt;
              &lt;Setter Property="ItemTemplate" Value="{StaticResource normal}" /&gt;
              &lt;Style.Triggers&gt;
                  &lt;Trigger Property="IsSelected" Value="True"&gt;
                      &lt;Setter Property="ItemTemplate" Value="{StaticResource selected}" /&gt;
                  &lt;/Trigger&gt;
              &lt;/Style.Triggers&gt;
          &lt;/Style&gt;
      &lt;/Window.Resource&gt;
      &lt;Grid&gt;
          &lt;TreeView ItemSource="{Binding Body}" ItemContainerStyle="{StaticResource ContainerStyle}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>What happens is that there is only one node in the tree, and the text of the node is the type name of the object. It sounds like the type bound to the node isn't what the template is expecting, so it's using the default <code>ToString()</code> binding instead of the <code>Text</code> property as I specified.</p>
  
  <p>I have set the DataContext of the Window in the code behind file. I know that my Bindings for the data are correct, because if I set one <code>HierarchicalDataTemplate</code> for the TreeView the data is displayed correctly.</p>
  
  <p>I think that my problem is that I need to set a property other than <code>ItemTemplate</code> in the <code>TreeViewItem</code> styles - am I using the right property, or should I set something else?</p>