How to add ContextMenu column in WPF DataGrid

I am trying to add a context menu to a column in a WPat datagrid and do not quite understand how this is possible. I know how to add it to a datagrid, but I would like to have different menu items based on the column, and also so that the menu click event knew about the column or even better than the cell in which the context menu was selected.

My ultimate goal is to create a context menu that has a β€œClear” menu item that will be used to exclude data in this column. I do not want an empty row or a lie in the case of a checkbox column, I want the underlying data to be muted. If anyone has a suggestion on a reusable way to do this, that would be greatly appreciated.

Thanks a lot!

+3
source share
2 answers

Another option is to set the column headings to a TextBlock (or some other control that ContextMenu can handle).

So you could say:

 // Create a context menu var cm = new ContextMenu(); cm.Items.Add(new MenutItem{Header="SampleItem"}); // Create a textblock with your header text and link the context menu var tb = new TextBlock{Text="My Column Name"}; tb.ContextMenu = cm; // Set the grid column header to your textblock grid.Columns[0].Header=tb; 
+2
source

I am adding a context menu to cells like this

  <!--Cell ContextMenu--> <ContextMenu x:Key="cellContextMenu"> <MenuItem x:Name="menuFillUp" Click="menuFillUp_Click" Header="Fill _Up" /> <MenuItem x:Name="menuFillDown" Click="menuFillDown_Click" Header="Fill _Down" /> </ContextMenu> 

and set it like this

  <!--DataCell Style - Adds ContextMenu--> <Style TargetType="{x:Type dg:DataGridCell}"> <Setter Property="ContextMenu" Value="{DynamicResource cellContextMenu}" /> </Style> 

then I add the context menu to the column header using the following context menu

  <!--Show Hidden Columns ContextMenu--> <DataTemplate x:Key="menuItemColumnShow"> <MenuItem x:Name="menuShowColumn" DataContext="{Binding Path=.}" Header="{Binding Path=Tag.Code}" Click="menuShowColumn_Click" /> </DataTemplate> <!--Column ContextMenu--> <ContextMenu x:Key="columnContextMenu"> <MenuItem x:Name="menuHideColumn" Click="menuHideColumn_Click" Header="Hide _Column" /> <MenuItem x:Name="showMenu" Header="Show" ItemsSource="{Binding RelativeSource={RelativeSource AncestorType={x:Type local:ResultEditorGrid}}, Path=HiddenColumnCollection}" ItemTemplate="{StaticResource menuItemColumnShow}"> <MenuItem.Style> <Style BasedOn="{StaticResource {x:Type MenuItem}}" TargetType="{x:Type MenuItem}"> <Style.Triggers> <DataTrigger Binding="{Binding RelativeSource={RelativeSource self}, Path=Items.Count}" Value="0"> <Setter Property="Visibility" Value="Collapsed" /> </DataTrigger> </Style.Triggers> </Style> </MenuItem.Style> </MenuItem> <MenuItem Header="_Add Item" Command="local:MyCommands.AddTItem" CommandTarget="{Binding Path=PlacementTarget, RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type ContextMenu}}}" /> </ContextMenu> <!--ColumnManagerCell Style - Adds ContextMenu - Tool Tip Style for Marking Scheme Preview--> <Style TargetType="{x:Type dg:DataGridColumnHeader}"> <Setter Property="ContextMenu" Value="{DynamicResource columnContextMenu}" /> </Style> 

(im, using wierd mix of commands and click events, the type of code evolves :) that the above code gives me dynamic menus in the column headers. I have a hidden collection of columns on my derived grid, and I show that the shich columns are hidden / shown in the context menu in the column, and I have a separate context menu for the cells in the grid.

you can find which column was clicked

  private void menuHideColumn_Click(object sender, RoutedEventArgs e) { //move thru the visual tree to get the context menu DependencyObject dep = (DependencyObject)e.OriginalSource; while ((dep != null) && !(dep is ContextMenu)) { dep = VisualTreeHelper.GetParent(dep); } if (dep == null) { } else { //we have a context menu, cast it ContextMenu contextMenu = (ContextMenu)dep; if (contextMenu.PlacementTarget is DataGridColumnHeader) { DataGridColumnHeader header = contextMenu.PlacementTarget as DataGridColumnHeader; //get the grid to hide the column myDerivedGrid.HideColumn(header.Column); } } } 

I may have missed a few things, feel free to comment and poorly try to fill in the details.

+1
source

Source: https://habr.com/ru/post/1492969/


All Articles