title: Command binding not working in a dynamic MVVM Context Menu
body: <p>I am new to WPF. Like many others, I am trying to bind a <code>ContextMenu</code> to an <code>ObservableCollection</code> to create a dynamic context menu. 
  Everything works except binding the <code>Command</code> property to the <code>TheCommand</code> property of the <code>MenuItemViewModel</code> class, that represents the menu item. The command is not fired. What am I doing wrong?</p>
  
  <p>To start from the beginning, the <code>ContextMenu</code> is a child of the <code>Image</code> and is shown when the mouse is over the <code>Image</code>. </p>
  
  <pre><code> &lt;Image.ContextMenu &gt;
          &lt;ContextMenu ItemsSource="{DynamicResource ContextMenu}"
  </code></pre>
  
  <p>where the empty ContextMenu is defined as follows:</p>
  
  <pre><code>&lt;Window.Resources&gt;
      &lt;local:MenuItemViewModelCollection x:Key="ContextMenu"&gt;
      &lt;/local:MenuItemViewModelCollection&gt;
  
      &lt;HierarchicalDataTemplate DataType="{x:Type local:MenuItemViewModel}"
                                        ItemsSource="{Binding Path=Children}"&gt;
          &lt;HierarchicalDataTemplate.ItemContainerStyle&gt;
              &lt;Style TargetType="MenuItem"&gt;
                  &lt;Setter Property="Command"
                      Value="{Binding RelativeSource={RelativeSource AncestorType={x:Type ContextMenu}},
                                      Path=DataContext.TheCommand}"/&gt;
                &lt;!--  Value="{Binding Path=TheCommand}" /&gt; I tried this too --&gt;
  
              &lt;/Style&gt;
          &lt;/HierarchicalDataTemplate.ItemContainerStyle&gt;
      &lt;/HierarchicalDataTemplate&gt;
  &lt;/Window.Resources&gt;
  </code></pre>
  
  <p>The <code>TheCommand</code> property is defined below:</p>
  
  <pre class="lang-cs prettyprint-override"><code>public class MenuItemViewModel : INotifyPropertyChanged
  {
         //...
         public ICommand TheCommand
         {
               //...
         }
  }
  </code></pre>