title: Binding a TreeView with ContextMenu in Xaml
body: <p>I'm pretty new to Xaml and need some advise.</p>
  
  <p>A TreeView should be bound to a hierarchical object structure. The TreeView should have a context menu, which is specific for each object type.</p>
  
  <p>I've tried the following:</p>
  
  <pre><code>&lt;TreeView&gt;
    &lt;TreeView.Resources&gt;
      &lt;DataTemplate x:Key="RoomTemplate"&gt;
        &lt;TreeViewItem Header="{Binding Name}"&gt;
          &lt;TreeViewItem.ContextMenu&gt;
            &lt;ContextMenu&gt;
              &lt;MenuItem Header="Open" /&gt;
              &lt;MenuItem Header="Remove" /&gt;
            &lt;/ContextMenu&gt;
          &lt;/TreeViewItem.ContextMenu&gt;
        &lt;/TreeViewItem&gt;
      &lt;/DataTemplate&gt;
    &lt;/TreeView.Resources&gt;
  
    &lt;TreeViewItem Header="{Binding Name}" Name="tviRoot" IsExpanded="True" &gt;
  
    &lt;TreeViewItem Header="Rooms"  
                  ItemsSource="{Binding Rooms}"
                  ItemTemplate="{StaticResource RoomTemplate}"&gt;
      &lt;TreeViewItem.ContextMenu&gt;
        &lt;ContextMenu&gt;
          &lt;MenuItem Header="Add room"&gt;&lt;/MenuItem&gt;
        &lt;/ContextMenu&gt;
      &lt;/TreeViewItem.ContextMenu&gt;
    &lt;/TreeViewItem&gt;
  &lt;/TreeViewItem&gt;
  </code></pre>
  
  <p></p>
  
  <p>But with this markup the behavior is as intended, but the child items (the rooms) are indented too much.</p>
  
  <p>Anyway all the bining samples I could find use TextBlock instead of TreeViewItem in the DataTemplate, but wonder how to integrate the ContextMenu there.</p>