如何在右键单击时获取数据网格列索引? MVVM WPF

How to get Data Grid column index on right click? MVVM WPF

我想得到 column Index 并通过 CommandParameter 我有 context menu for Data grid column header.

<DataGrid.Resources>
     <ContextMenu x:Key="DataGridColumnHeaderContextMenu">
           <MenuItem Header="Freez Column"
                     Command="{Binding DataContext.FreezColumnCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                     CommandParameter="{Binding}" />
     </ContextMenu>
     <Style TargetType="{x:Type DataGridColumnHeader}">
           <Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}" />
     </Style>
</DataGrid.Resources>

我试图通过在 CommandParameter 中传递 Binding 来传递整个 object,但它只传递了 header 文本。

并尝试使用RelateveSource AncestorType=DataGridColumn仍然无法弄清楚。

编辑:

数据网格代码:

<DataGrid ItemsSource="{Binding MyList}" HorizontalScrollBarVisibility="Visible" CanUserAddRows="False" AutoGenerateColumns="False">
     <DataGrid.Resources>
          <ContextMenu x:Key="DataGridColumnHeaderContextMenu">
               <MenuItem Header="Freez Column"
                         Command="{Binding DataContext.FreezColumnCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                         CommandParameter="{Binding}" />
          </ContextMenu>
          <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}" />
          </Style>
     </DataGrid.Resources>
     <DataGrid.Columns>
          <DataGridTextColumn Header="Story" Binding="{Binding Story}" />
          <DataGridCheckBoxColumn Header="Design" Binding="{Binding Design}" />
          <DataGridTextColumn Header="CadId" Binding="{Binding CadId}" />
     </DataGrid.Columns>
</DataGrid>

发生这种情况可能是因为 ContextMenu 是在单独的 VisualTree 中维护的(如果我错了请纠正我)。请使用以下代码获取 DisplayIndex,

  <DataGrid
        x:Name="myGrid"
        AutoGenerateColumns="False"
        CanUserAddRows="False"
        HorizontalScrollBarVisibility="Visible"
        ItemsSource="{Binding Persons}">
        <DataGrid.Resources>
            <local:GridToIndexConverter x:Key="GridToIndexConverter" />
            <ContextMenu x:Key="DataGridColumnHeaderContextMenu">
                <MenuItem
                    Command="{Binding DataContext.FreezColumnCommand, RelativeSource={RelativeSource AncestorType=DataGrid}}"
                    CommandParameter="{Binding PlacementTarget.DisplayIndex, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=ContextMenu}}"
                    Header="Freez Column" />
            </ContextMenu>
            <Style TargetType="{x:Type DataGridColumnHeader}">
                <Setter Property="ContextMenu" Value="{StaticResource DataGridColumnHeaderContextMenu}" />
            </Style>
        </DataGrid.Resources>
        <DataGrid.Columns>
            <DataGridTextColumn Binding="{Binding Story}" Header="Story" />
            <DataGridCheckBoxColumn Binding="{Binding Design}" Header="Design" />
            <DataGridTextColumn Binding="{Binding CadId}" Header="CadId" />
        </DataGrid.Columns>
    </DataGrid>

我访问了 ContextMenuPlacementTarget