title: Adding context menu to leaf node in TreeView WPF
body: <p>In my WPF application, I want to add a context menu and its handler in <code>ViewModel</code> to the leaf nodes of my <code>TreeView</code> control. Here is how I have added the <code>TreeView</code> Control.</p>
  
  <pre><code>&lt;TreeView Name="treePads" ItemsSource="{Binding pads}" Width="190"&gt;
      &lt;TreeView.Resources&gt;
          &lt;HierarchicalDataTemplate DataType="{x:Type self:Pad}" ItemsSource="{Binding Members}"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="{Binding Name}" /&gt;
                  &lt;TextBlock Text=" [" Foreground="Blue" /&gt;
                  &lt;TextBlock Text="{Binding Members.Count}" Foreground="Blue" /&gt;
                  &lt;TextBlock Text="]" Foreground="Blue" /&gt;
              &lt;/StackPanel&gt;
          &lt;/HierarchicalDataTemplate&gt;
  
          &lt;DataTemplate DataType="{x:Type self:PadInfo}"&gt;
              &lt;StackPanel Orientation="Horizontal"&gt;
                  &lt;TextBlock Text="["&gt;&lt;/TextBlock&gt;
                  &lt;TextBlock Text="{Binding SlotID}" /&gt;
                  &lt;TextBlock Text="] ["&gt;&lt;/TextBlock&gt;
                  &lt;TextBlock Text="{Binding WellID}" /&gt;
                  &lt;TextBlock Text="]"&gt;&lt;/TextBlock&gt;
              &lt;/StackPanel&gt;
          &lt;/DataTemplate&gt;
      &lt;/TreeView.Resources&gt;
  &lt;/TreeView&gt;
  </code></pre>
  
  <p>Here is how the output of this code looks like:</p>
  
  <p><img src="https://i.stack.imgur.com/mkoDU.png" alt=""></p>
  
  <p>I want to add a <code>ContextMenu</code> having two options: <code>Rename</code>, <code>Delete</code> and add there event handlers to the <code>ViewModel</code>. How can I do that? </p>