title: How to get Data Grid column index on right click? MVVM WPF
body: <p>I want to get <code>column Index</code> and pass it by <code>CommandParameter</code> I have, in a <code>context menu</code> for <code>Data grid column header</code>.</p>
  
  <pre><code>&lt;DataGrid.Resources&gt;
       &lt;ContextMenu x:Key="DataGridColumnHeaderContextMenu"&gt;
             &lt;MenuItem Header="Freez Column"
                       Command="{Binding DataContext.FreezColumnCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                       CommandParameter="{Binding}" /&gt;
       &lt;/ContextMenu&gt;
       &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
             &lt;Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}" /&gt;
       &lt;/Style&gt;
  &lt;/DataGrid.Resources&gt;
  </code></pre>
  
  <p>I've tried to pass the entire object by passing <code>Binding</code> in <code>CommandParameter</code>, but it only passes the header text.</p>
  
  <p>And tried using <code>RelateveSource AncestorType=DataGridColumn</code> still couldn't figure it out.</p>
  
  <p><strong>Edit:</strong></p>
  
  <p>DataGrid code:</p>
  
  <pre><code>&lt;DataGrid ItemsSource="{Binding MyList}" HorizontalScrollBarVisibility="Visible" CanUserAddRows="False" AutoGenerateColumns="False"&gt;
       &lt;DataGrid.Resources&gt;
            &lt;ContextMenu x:Key="DataGridColumnHeaderContextMenu"&gt;
                 &lt;MenuItem Header="Freez Column"
                           Command="{Binding DataContext.FreezColumnCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                           CommandParameter="{Binding}" /&gt;
            &lt;/ContextMenu&gt;
            &lt;Style TargetType="{x:Type DataGridColumnHeader}"&gt;
                  &lt;Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}" /&gt;
            &lt;/Style&gt;
       &lt;/DataGrid.Resources&gt;
       &lt;DataGrid.Columns&gt;
            &lt;DataGridTextColumn Header="Story" Binding="{Binding Story}" /&gt;
            &lt;DataGridCheckBoxColumn Header="Design" Binding="{Binding Design}" /&gt;
            &lt;DataGridTextColumn Header="CadId" Binding="{Binding CadId}" /&gt;
       &lt;/DataGrid.Columns&gt;
  &lt;/DataGrid&gt;
  </code></pre>