title: How to bind a Command to a ContextMenu from within an ItemTemplate?
body: <p>I want to bind a certain command to a <code>menuItem</code>. The said menu item is part of a <code>ContextMenu</code> that is defined inside an <code>ItemTemplate</code>.</p>
  
  <p>Right now, what I have compiles and runs, but the command is never called. In the past, I had used a similar pattern to hook commands to buttons defined in an <code>ItemTemplate</code> with success.</p>
  
  <p>Anyone has any idea how I could accomplish this?</p>
  
  <p>XAML:</p>
  
  <pre><code>&lt;Window
      xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
      xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
      xmlns:local="clr-namespace:Wpf_treeView" x:Name="window" x:Class="Wpf_treeView.MainWindow"
      Title="MainWindow" Height="350" Width="525"&gt;
    &lt;Grid&gt;
      &lt;TreeView HorizontalAlignment="Left" Height="299" Margin="10,10,0,0" VerticalAlignment="Top" Width="228" ItemsSource="{Binding DataInfosView}" &gt;
        &lt;TreeView.ItemTemplate&gt;
          &lt;HierarchicalDataTemplate ItemsSource="{Binding Children}"&gt;
            &lt;TextBlock DockPanel.Dock="Left" Text="{Binding InfoValue}" TextAlignment="Left" &gt;
              &lt;TextBlock.ContextMenu&gt;
                &lt;ContextMenu&gt;
                  &lt;MenuItem Header="{Binding InfoValue}" IsEnabled="False"/&gt;
                  &lt;MenuItem Header="Add child" Command="{Binding AddChildCmd, ElementName=window}"/&gt;
                &lt;/ContextMenu&gt;
              &lt;/TextBlock.ContextMenu&gt;
            &lt;/TextBlock&gt;
          &lt;/HierarchicalDataTemplate&gt;
        &lt;/TreeView.ItemTemplate&gt;
      &lt;/TreeView&gt;
    &lt;/Grid&gt;
  &lt;/Window&gt;
  </code></pre>
  
  <p>C#:</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Windows;
  using System.Windows.Data;
  using System.Windows.Input;
  
  namespace Wpf_treeView
  {
      public partial class MainWindow : Window
      {
          private static readonly Random rnd = new Random();
          private List&lt;InfoData&gt; m_InfoData = new List&lt;InfoData&gt;();
  
          public ListCollectionView DataInfosView { get; private set; }
  
          public static readonly DependencyProperty AddChildProperty =
              DependencyProperty.Register("AddChildCmd", 
                                          typeof(ICommand),
                                          typeof(MainWindow));
  
          public ICommand AddChildCmd
          {
              get { return (ICommand) GetValue(AddChildProperty); }
              set { SetValue(AddChildProperty, value); }
          }
  
          public MainWindow()
          {
              AddChildCmd = new RoutedCommand();
              CommandManager.RegisterClassCommandBinding(
                  GetType(), 
                  new CommandBinding(AddChildCmd, AddChild));
  
              m_InfoData.Add(new InfoData(4));
              m_InfoData.Add(new InfoData(1));
              m_InfoData.Add(new InfoData(5));
              m_InfoData[1].Children.Add(new InfoData(3));
              m_InfoData[1].Children[0].Children.Add(new InfoData(7));
  
              DataInfosView = new ListCollectionView(m_InfoData);
              DataContext = this;
  
              InitializeComponent();
          }
  
          private void AddChild(object sender, RoutedEventArgs e)
          {
              ExecutedRoutedEventArgs args = (ExecutedRoutedEventArgs)e;
              InfoData info = (InfoData)args.Parameter;
              info.Children.Add(new InfoData(rnd.Next(0, 11)));
          }
      }
  
      class InfoData : INotifyPropertyChanged
      {
          private int infoValue;
  
          public int InfoValue
          {
              get { return infoValue; }
              set
              {
                  if (value != infoValue)
                  {
                      infoValue = value;
                      OnPropertyChanged();
                  }
              }
          }
  
          public List&lt;InfoData&gt; Children { get; private set; }
  
          public InfoData(int infoValue)
          {
              InfoValue = infoValue;
              Children = new List&lt;InfoData&gt;();
          }
  
          public event PropertyChangedEventHandler PropertyChanged;
  
          protected virtual void OnPropertyChanged(
              [CallerMemberName] string propertyName = null)
          {
              if (PropertyChanged != null)
                  PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
          }
      }
  }
  </code></pre>