How to set different DataGridColumnHeader has different ContextMenu? #17743
-
#15252 provided a method, but the exact same ContextMenu will appear for all DataGridColumnHeaders.
Is there a way to set or not set the ContextMenu and set different ContextMenus for different headers? Thanks. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 4 replies
-
Why not just set it in xaml like this? Copypaste from ControlCatalog DataGridPage.xaml and adding a contextmenu to header: <DataGrid Name="dataGridGrouping" Margin="12">
<DataGrid.Columns>
<DataGridTextColumn
Width="6*"
x:DataType="local:Country"
Binding="{Binding Name}"
Header="Country or Region" />
<DataGridTextColumn
Width="4*"
x:DataType="local:Country"
Binding="{Binding Region}">
<DataGridTextColumn.Header>
<TextBlock Text="Region">
<TextBlock.ContextMenu>
<ContextMenu>
<MenuItem Header="Test Header ContextMenu" />
</ContextMenu>
</TextBlock.ContextMenu>
</TextBlock>
</DataGridTextColumn.Header>
</DataGridTextColumn>
<!-- rest of datagrid -->
</DataGrid> |
Beta Was this translation helpful? Give feedback.
-
Thank you for your reply. As you can see, this effect makes the header wider and unsightly. And right-clicking on both sides of the header will not bring up the ContextMenu, instead of right-clicking anywhere in the DataGridColumnHeader, which will cause confusion to the user and think that there is something wrong. |
Beta Was this translation helpful? Give feedback.
Alternative way, if your header content is a constant:
Possible a custom handling of
Control.ContextRequestedEvent
could help as well -- but I do not see a reliable way of identifying which header is clicked, other then checking the header text, as most information about it is marked internal.