title: WPF DatGrid Context Menu Binding
body: <p>I have a WPF DataGrid with a DataContext that is bound to an Observable collection of view models.</p>
  
  <p>My DataGrid columns are each bound to a different property of the objects in the collection and this works correctly, displaying the objects in the collection on the grid.</p>
  
  <p>I want to add a context menu onto this grid, with the menu items defined as another property on the view model that make up the overall observable collection.  These menu items are another Observable collection of menu items.  I know this approach can work as I have similar code on a treeview in another part of the application.</p>
  
  <p>I have defined the binding of the DataGrid and ContextMenu as follows:</p>
  
  <pre><code> &lt;DataGrid Name="SynchErrors" Grid.Row="1"
                Style="{StaticResource SortableGrid}"
                ItemsSource="{Binding}"&gt;
          &lt;DataGrid.Resources&gt;
              &lt;userControls:BindingProxy x:Key="BindingProxy" DataContextProxy="{Binding}"  /&gt;
  
          &lt;/DataGrid.Resources&gt;
          &lt;DataGrid.ContextMenu&gt;
               &lt;ContextMenu DataContext="{Binding Path=DataContextProxy, Mode=TwoWay, Source={StaticResource BindingProxy}}"
              ItemsSource="{Binding Path=DataContextProxy.ContextMenuCommandList, Mode=TwoWay, Source={StaticResource BindingProxy}}"&gt;
                  &lt;ContextMenu.Resources&gt;
                      &lt;Style TargetType="{x:Type MenuItem}"&gt;
                          &lt;Setter Property="Command" Value="{Binding ContextMenuCommand}"&gt;&lt;/Setter&gt;
                          &lt;Setter Property="Header" Value="{Binding DisplayName}"&gt;&lt;/Setter&gt;
                      &lt;/Style&gt;
                  &lt;/ContextMenu.Resources&gt;
              &lt;/ContextMenu&gt;
          &lt;/DataGrid.ContextMenu&gt;
  
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="From"
                                  MinWidth="120"
                                  Width="*"
                                  Binding="{Binding Path=Owner}"/&gt;
  
              &lt;DataGridTextColumn Header="Subject"
                                  Width="2*"
                                  Binding="{Binding Path=Name}"
                                  SortMemberPath="Name"/&gt;
         &lt;DataGrid.Columns&gt;
  </code></pre>
  
  <p>When I run my application, I get my items listed correctly in the grid, but when I right click, I get an empty context menu and the following binding error:</p>
  
  <blockquote>
    <p>System.Windows.Data Error: 40 : BindingExpression path error: 'ContextMenuCommandList' property not found on 'object' ''ObservableCollection`1' (HashCode=53690177)'. BindingExpression:Path=DataContextProxy.ContextMenuCommandList; DataItem='BindingProxy' (HashCode=43857660); target element is 'ContextMenu' (Name=''); target property is 'ItemsSource' (type 'IEnumerable')</p>
  </blockquote>
  
  <p>I have used the BindingProxy to pass the DataContext from the DataGrid to the ContextMenu as the context menu is outside the Visual Tree, however this appears to be passing the entire collection rather than the individual items bound to each row.</p>
  
  <p>Is it possible to define the binding of a context menu on a DataGrid to reference each individual item of the parent collection?</p>