title: How do I use the DataType property on a WPF DataTemplate?
body: <p>So obviously I am doing something wrong, but I just cannot seem to get the HierarchicalDataTemplate (or even just DataTemplate) to work when using the DataType property.  I have created the shortest possible WPF application to demonstrate the problem.</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.Window1"
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:System="clr-namespace:System;assembly=mscorlib"
      xmlns:local="clr-namespace:WpfApplication1"
      Title="Window1" Height="300" Width="300" Loaded="Window_Loaded"&gt;
      &lt;Window.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="x:Type local:Foo"&gt;
              &lt;TextBlock Text="I am a Foo" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
          &lt;HierarchicalDataTemplate DataType="x:Type System:String"&gt;
              &lt;TextBlock Text="I am a String" /&gt;
          &lt;/HierarchicalDataTemplate&gt;
      &lt;/Window.Resources&gt;
      &lt;Grid&gt;
          &lt;TreeView Name="treeView1" ItemsSource="{Binding}" /&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>CODE:</p>
  
  <pre><code>namespace WpfApplication1
  {
      public class Foo
      {
          public string Name { get; set; }
      }
  
      public partial class Window1 : Window
      {
          public Window1()
          {
              InitializeComponent();
          }
  
          private void Window_Loaded(object sender, RoutedEventArgs e)
          {
              var list = new List&lt;object&gt; { "a", 1, "b", 2, new Foo() { Name="Brian"}};
              treeView1.DataContext = list;
          }
      }
  }
  </code></pre>
  
  <p>Obviously I am expecting it display the following in the treeview.</p>
  
  <pre><code>I am a string
  1
  I am a string
  2
  I am a foo
  </code></pre>
  
  <p>But my application actually displays the following.</p>
  
  <pre><code>a
  1
  b
  2
  WpfApplication1.Foo
  </code></pre>
  
  <p>The strange thing is that almost every example I see on the web does this very thing (with slight variations) and no one else seems to be having a problem with it.  Yet I have tried countless different ways of rearranging the XAML and nothing seems to help.  I am hoping another pair eyes can help.</p>