I want to get column Index
and pass it by CommandParameter
I have, in a 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>
I've tried to pass the entire object by passing Binding
in CommandParameter
, but it only passes the header text.
And tried using RelateveSource AncestorType=DataGridColumn
still couldn't figure it out.
Edit:
DataGrid code:
<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>