title: WPF Heirachical DataTemplate in TreeView
body: <p>I am trying to get my head around Heirarchical DataTemplates and TreeViews in WPF and am having some trouble.</p>
  
  <p>I have created an app with only a TreeView on the form as below and defined HierarchicalDataTemplate's for both a Directory object and a File object I then Bind the TreeView to the Directories property (ObservableCollection) of my model.</p>
  
  <pre><code>&lt;Grid&gt;
          &lt;Grid.Resources&gt;
  
              &lt;HierarchicalDataTemplate DataType="{x:Type local:Directory}" ItemsSource ="{Binding Directories}"&gt;
                  &lt;TextBlock Text="{Binding Path=Name}"/&gt;
              &lt;/HierarchicalDataTemplate&gt;
              &lt;HierarchicalDataTemplate DataType="{x:Type local:File}" ItemsSource ="{Binding Files}"&gt;
                  &lt;TextBlock Text="{Binding Path=FileName}"/&gt;
              &lt;/HierarchicalDataTemplate&gt;
          &lt;/Grid.Resources&gt;
          &lt;TreeView Margin="12,12,0,12" Name="treeView1" HorizontalAlignment="Left" Width="204" &gt;
              &lt;TreeViewItem ItemsSource="{Binding Directories}" Header="Folder Structure" /&gt;
          &lt;/TreeView&gt;
      &lt;/Grid&gt;
  </code></pre>
  
  <p>This works in that in the TreeView I see my directories and it recursively displays all sub directories, but what I want to see is Directories and Files!  I've checked the model and it definately has files in some of the sub directories but I can't see them in the tree.</p>
  
  <p>I'm not sure if it is my template that is the problem or my model so I have included them all! :-)</p>
  
  <p>Thanks</p>
  
  <p>OneShot</p>
  
  <pre><code>public partial class MainWindow : Window
  {
  
      public MainWindow()
      {
          InitializeComponent();
  
      }
  
  
      private MainWindowViewModel _vm;
  
      public MainWindowViewModel VM
      {
          set
          {
              _vm = value;
              this.DataContext = _vm;
          }
      }
  
      private void Window_Loaded(object sender, RoutedEventArgs e)
      {
          var d = new Directory() { Name = "temp" };
          recurseDir("c:\\temp", ref d);
  
          VM = new MainWindowViewModel( new List&lt;Directory&gt;() { d } );            
      }
  
      private void recurseDir(string path, ref Directory dir)
      {
          var files = System.IO.Directory.GetFiles(path);
          var dirs = System.IO.Directory.GetDirectories(path);
  
          dir.Name = path.Substring(path.LastIndexOf("\\")+1);
  
          for (int i = 0; i &lt; files.Length; i++)
          {
              var fi = new FileInfo(files[i]);
              dir.Files.Add(new File() { 
                  FileName = System.IO.Path.GetFileName(files[i]), 
                  DirectoryPath = System.IO.Path.GetDirectoryName(files[i]), 
                  Size = fi.Length, 
                  Extension= System.IO.Path.GetExtension(files[i]) 
              });
  
          }
  
          for (int i = 0; i &lt; dirs.Length; i++)
          {
              var d = new Directory() { Name = dirs[i].Substring(dirs[i].LastIndexOf("\\")+1) };
              recurseDir(dirs[i], ref d);
              dir.Directories.Add(d);
  
          }
  
      }
  }
  </code></pre>
  
  <p>-</p>
  
  <pre><code> public class MainWindowViewModel
          : DependencyObject
      {
  
  
          public MainWindowViewModel(List&lt;Directory&gt; Dirs)
          {
              this.Directories = new ObservableCollection&lt;Directory&gt;( Dirs);
          }
  
          public ObservableCollection&lt;Directory&gt; Directories
          {
              get { return (ObservableCollection&lt;Directory&gt;)GetValue(DirectoriesProperty); }
              set { SetValue(DirectoriesProperty, value); }
          }
  
          public static readonly DependencyProperty DirectoriesProperty =
              DependencyProperty.Register("Directories", typeof(ObservableCollection&lt;Directory&gt;), typeof(MainWindowViewModel), new UIPropertyMetadata(null));
  
          public Directory BaseDir
          {
              get { return (Directory)GetValue(BaseDirProperty); }
              set { SetValue(BaseDirProperty, value); }
          }
  
          public static readonly DependencyProperty BaseDirProperty =
              DependencyProperty.Register("BaseDir", typeof(Directory), typeof(MainWindowViewModel), new UIPropertyMetadata(null));
  
  
      }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>public class Directory
      {
          public Directory()
          {
              Files = new List&lt;File&gt;();
              Directories = new List&lt;Directory&gt;();
          }
          public List&lt;File&gt; Files { get; private set; }
          public List&lt;Directory&gt; Directories { get; private set; }
          public string Name { get; set; }
          public int FileCount
          {
              get
              {
                  return Files.Count;
              }
          }
          public int DirectoryCount
          {
              get
              {
                  return Directories.Count;
              }
          }
          public override string ToString()
          {
              return Name;
          }
      }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>public class File
      {
          public string DirectoryPath { get; set; }
          public string FileName { get; set; }
          public string Extension { get; set; }
          public double Size { get; set; }
          public string FullPath
          {
              get
              {
                  return System.IO.Path.Combine(DirectoryPath, FileName);
              }
          }
          public override string ToString()
          {
              return FileName;
          }
      }
  </code></pre>