title: Why ContextMenu Command and CommandParameter are not working in DataGrid
body: <p>I have a <code>DataGird</code>, then it's include <code>Expander</code> and <code>ContextMenu</code>.</p>
  
  <p>This <code>DataGird</code> is grouping. And, it's has different <code>ContextMenu</code> between <code>Expander</code> and <code>DataGridItem</code>.</p>
  
  <p>However, I want to binding <code>MenuItem</code> <code>Command</code> to my command, and pass <code>DataGird</code>'s <code>SelectedItems</code> or <code>SelectedIndex</code> to <code>Command</code>.</p>
  
  <p>But this command is not invoked. Even if it work, the parameter is not pass when I survey some method got goal (like set property <code>tag</code>).</p>
  
  <p>So, I want to know what's correct method.</p>
  
  <p><strong>General</strong></p>
  
  <pre><code>&lt;UserControl.DataContext&gt;
      &lt;vm:ViewModel x:Name="VM"/&gt;
  &lt;/UserControl.DataContext&gt;
  &lt;Grid&gt;
      &lt;DataGrid x:Name="dg"
                ItemsSource="{Binding SourceData}"
                AutoGenerateColumns="False" 
                CanUserResizeColumns="False" 
                CanUserResizeRows="False"
                CanUserAddRows="False" 
                CanUserSortColumns="False"
                SelectionMode="Extended"
                RowHeaderWidth="0"
                GridLinesVisibility="Horizontal"&gt;
          &lt;DataGrid.CellStyle&gt;
              &lt;Style TargetType="DataGridCell"&gt;
                  &lt;Setter Property="BorderThickness" Value="0"/&gt;
                  &lt;!-- Remove the focus indication for the selected cell --&gt;
                  &lt;Setter Property="FocusVisualStyle" Value="{x:Null}"/&gt;
              &lt;/Style&gt;
          &lt;/DataGrid.CellStyle&gt;
          &lt;DataGrid.Columns&gt;
              &lt;DataGridTextColumn Header="No." Binding="{Binding No}" /&gt;
              &lt;DataGridTextColumn Header="Name" Binding="{Binding Name}"/&gt;
          &lt;/DataGrid.Columns&gt;
          &lt;DataGrid.GroupStyle&gt;
              &lt;GroupStyle&gt;
                  &lt;GroupStyle.ContainerStyle&gt;
                      &lt;Style TargetType="{x:Type GroupItem}"&gt;
                          &lt;Setter Property="Template"&gt;
                              &lt;Setter.Value&gt;
                                  &lt;ControlTemplate&gt;
                                      &lt;Expander IsExpanded="False"  MouseRightButtonDown="Expander_MouseRightButtonDown" ButtonBase.Click="Expander_Click"&gt;
                                          &lt;Expander.Header&gt;
                                              &lt;StackPanel Orientation="Horizontal"&gt;
                                                  &lt;StackPanel.ContextMenu&gt;
                                                      &lt;ContextMenu&gt;
                                                          &lt;MenuItem Header="Insert" InputGestureText="Ctrl+I" Command="{Binding InsertGroupItems"/&gt;
                                                          &lt;MenuItem Header="Remove" InputGestureText="Ctrl+D" Command="{Binding RemoveGroupItems"/&gt;
                                                      &lt;/ContextMenu&gt;
                                                  &lt;/StackPanel.ContextMenu&gt;
                                                  &lt;TextBlock Text="{Binding Name}"/&gt;
                                                  &lt;TextBlock Text="{Binding ItemCount}"/&gt;
                                                  &lt;TextBlock Text=" item(s)"/&gt;
                                              &lt;/StackPanel&gt;
                                          &lt;/Expander.Header&gt;
                                          &lt;ItemsPresenter&gt;
                                              &lt;ItemsPresenter.ContextMenu&gt;
                                                  &lt;ContextMenu&gt;
                                                      &lt;ContextMenu&gt;
                                                          &lt;MenuItem Header="Insert" InputGestureText="Ctrl+I" Command="{Binding InsertSelectedItems" CommandParameter="{Binding ElementName=dg, Path=SelectedIndex}"/&gt;
                                                          &lt;MenuItem Header="Remove" InputGestureText="Ctrl+D" Command="{Binding RemoveSelectedItems" CommandParameter="{Binding ElementName=dg, Path=SelectedItems}"/&gt;
                                                      &lt;/ContextMenu&gt;
                                              &lt;/ItemsPresenter.ContextMenu&gt;
                                          &lt;/ItemsPresenter&gt;
                                      &lt;/Expander&gt;
                                  &lt;/ControlTemplate&gt;
                              &lt;/Setter.Value&gt;
                          &lt;/Setter&gt;
                      &lt;/Style&gt;
                  &lt;/GroupStyle.ContainerStyle&gt;
              &lt;/GroupStyle&gt;
          &lt;/DataGrid.GroupStyle&gt;
      &lt;/DataGrid&gt;
  &lt;/Grid&gt;
  </code></pre>
  
  <p><strong>This work to invoke command, but can't pass parameter</strong></p>
  
  <pre><code>&lt;DataGrid.GroupStyle&gt;
              &lt;GroupStyle&gt;
                  &lt;GroupStyle.ContainerStyle&gt;
                      &lt;Style TargetType="{x:Type GroupItem}"&gt;
                          &lt;Style TargetType="{x:Type GroupItem}"&gt;
                              &lt;Setter Property="Tag" Value="{Binding DataContext, RelativeSource={RelativeSource AncestorType=DataGrid}}" /&gt;
                              &lt;Setter Property="ContextMenu"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ContextMenu&gt;
                                          &lt;MenuItem Header="Insert" InputGestureText="Ctrl+I" Command="{Binding PlacementTarget.Tag.InsertSelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandParameter="{Binding ElementName=dg, Path=SelectedIndex}"/&gt;
                                          &lt;MenuItem Header="Remove" InputGestureText="Ctrl+D" Command="{Binding PlacementTarget.Tag.RemoveSelectedItems, RelativeSource={RelativeSource AncestorType=ContextMenu}}" CommandParameter="{Binding ElementName=dg, Path=SelectedItems}"/&gt;
                                      &lt;/ContextMenu&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                              &lt;Setter Property="Template"&gt;
                                  &lt;Setter.Value&gt;
                                      &lt;ControlTemplate&gt;
                                          &lt;Expander IsExpanded="False"  MouseRightButtonDown="Expander_MouseRightButtonDown" ButtonBase.Click="Expander_Click"&gt;
                                              &lt;Expander.Header&gt;
                                                  &lt;StackPanel Orientation="Horizontal"&gt;
                                                      &lt;StackPanel.ContextMenu&gt;
                                                          &lt;ContextMenu&gt;
                                                              &lt;MenuItem Header="Insert" InputGestureText="Ctrl+I" Command="{Binding InsertGroupItems}"/&gt;
                                                              &lt;MenuItem Header="Remove" InputGestureText="Ctrl+D" Command="{Binding RemoveGroupItems}"/&gt;
                                                          &lt;/ContextMenu&gt;
                                                      &lt;/StackPanel.ContextMenu&gt;
                                                      &lt;TextBlock Text="{Binding Name}"/&gt;
                                                      &lt;TextBlock Text="{Binding ItemCount}"/&gt;
                                                      &lt;TextBlock Text=" item(s)"/&gt;
                                                  &lt;/StackPanel&gt;
                                              &lt;/Expander.Header&gt;
                                              &lt;ItemsPresenter/&gt;
                                          &lt;/Expander&gt;
                                      &lt;/ControlTemplate&gt;
                                  &lt;/Setter.Value&gt;
                              &lt;/Setter&gt;
                      &lt;/Style&gt;
                  &lt;/GroupStyle.ContainerStyle&gt;
              &lt;/GroupStyle&gt;
          &lt;/DataGrid.GroupStyle&gt;
  </code></pre>
  
  <p><em>BTW, how to cast <code>SelectedItems</code> when got parameter?</em></p>
  
  <p><strong>It's not work</strong></p>
  
  <pre><code>private void Excute(object parameter)
  {
      IList list = parameter as IList;
      foreach (var item in list)
      {
          Remove((Data)item);
      }
  }
  </code></pre>
  
  <p>So, maybe I have three problems.</p>
  
  <ol>
  <li><p>The <code>ContextMenu</code> not working.</p></li>
  <li><p>The <code>CommandParameter</code> not passing.</p></li>
  <li><p>How to convert <code>SelectedItems</code> to a list?</p></li>
  </ol>
  
  <p><strong>Thanks!</strong></p>