title: Databinding to TreeView in XAML
body: <p>I am trying to use WPF databinding features to get a <code>TreeView</code> to display a hierarchical tree of objects (categories). </p>
  
  <p>I have roughly followed <a href="http://www.codeproject.com/Articles/26288/Simplifying-the-WPF-TreeView-by-Using-the-ViewMode" rel="nofollow">this tutorial by Josh Smith</a>, but to no effect: no items appear in my <code>TreeView</code>.</p>
  
  <p>Here's complete code of my extremely simple application:</p>
  
  <pre><code>using System.Windows;
  
  namespace WpfApplication1
  {
      /// &lt;summary&gt;
      /// Interaction logic for MainWindow.xaml
      /// &lt;/summary&gt;
      public partial class MainWindow : Window
      {
          public MainWindow()
          {   
              InitializeComponent();
              this.DataContext = CategoriesTreeViewModel.CreateDefault;
          }
      }
  }
  </code></pre>
  
  <p>ViewModel objects and sample data:</p>
  
  <pre><code>using System.Collections.Generic;
  using System.Collections.ObjectModel;
  using System.Linq;
  
  namespace WpfApplication1
  {
      public class Category
      {
          public Category()
          {
              Children = new ObservableCollection&lt;Category&gt;();
          }
  
          public ObservableCollection&lt;Category&gt; Children
          {
              get;
              set;
          }
          public string Name { get; set; }
  
      }
  
      public class CategoriesTreeViewModel
      {
          public ReadOnlyCollection&lt;Category&gt; FirstGeneration;
  
          private static IEnumerable&lt;Category&gt; SomeCategories
          {
              get
              {
                  var A = new Category() { Name = "A" };
                  var B = new Category() { Name = "B" };
                  var A1 = new Category() { Name = "A1" };
                  var A2 = new Category() { Name = "A2" };
                  var B1 = new Category() { Name = "B1" };
                  var B2 = new Category() { Name = "B2" };
  
                  A.Children.Add(A1);
                  A.Children.Add(A2);
                  B.Children.Add(B1);
                  B.Children.Add(B2);
  
                  yield return A;
                  yield return B;
              }
          }
  
          public static CategoriesTreeViewModel CreateDefault
          {
              get
              {
                  var result = new CategoriesTreeViewModel()
                  {
                      FirstGeneration = new ReadOnlyCollection&lt;Category&gt;(SomeCategories.ToList())
                  };
                  return result;                
              }
          }
      }
  }
  </code></pre>
  
  <p>And XAML:</p>
  
  <pre><code>&lt;Window x:Class="WpfApplication1.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"&gt;
      &lt;Grid&gt;
          &lt;TreeView ItemsSource="{Binding FirstGeneration}" Name="treeView1"&gt;
              &lt;TreeView.ItemTemplate&gt;
                  &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt;
                      &lt;TextBlock Text="{Binding Name}" /&gt;
                  &lt;/HierarchicalDataTemplate&gt;
              &lt;/TreeView.ItemTemplate&gt;
          &lt;/TreeView&gt;
      &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>Why is the <code>TreeView</code> control blank?</p>