diff --git a/README.md b/README.md index d01ff23..7b18604 100644 --- a/README.md +++ b/README.md @@ -1,7 +1,72 @@ -# How to show different context menu for each column in WPF DataGrid (SfDataGrid)? +# How to Show Different Context Menu for each Column in WPF DataGrid? + +[WPF DataGrid](https://www.syncfusion.com/wpf-controls/datagrid) (SfDataGrid) allows you to set different [ContextMenu](https://learn.microsoft.com/en-us/dotnet/api/system.windows.controls.contextmenu?view=netframework-4.7.2) for each column using the [SfDataGrid.GridContextMenuOpening](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.SfDataGrid.html#Syncfusion_UI_Xaml_Grid_SfDataGrid_GridContextMenuOpening) event. -This example illustrates how to show different context menu for each column in [WPF DataGrid](https://www.syncfusion.com/wpf-ui-controls/datagrid) (SfDataGrid). +Refer to the following code example to define different ContextMenu for each column based on the column index in **SfDataGrid.GridContextMenuOpening** event. Here, the first three columns have different ContextMenu and the rest of columns have different ContextMenu, -[WPF DataGrid](https://www.syncfusion.com/wpf-ui-controls/datagrid) (SfDataGrid) allows you to set different ContextMenu for each column using the [SfDataGrid.GridContextMenuOpening](http://help.syncfusion.com/cr/cref_files/wpf/Syncfusion.SfGrid.WPF~Syncfusion.UI.Xaml.Grid.SfDataGrid~GridContextMenuOpening_EV.html) event. +```csharp +this.AssociatedObject.GridContextMenuOpening += AssociatedObject_GridContextMenuOpening; + +private void AssociatedObject_GridContextMenuOpening(object sender, GridContextMenuEventArgs e) +{ + e.ContextMenu.Items.Clear(); + var columnIndex = this.AssociatedObject.ResolveToGridVisibleColumnIndex(e.RowColumnIndex.ColumnIndex); -KB article - [How to show different context menu for each column in WPF DataGrid (SfDataGrid)?](https://www.syncfusion.com/kb/9840/how-to-show-different-context-menu-for-each-column-in-wpf-datagrid-sfdatagrid) + if (columnIndex < 0 && columnIndex >= this.AssociatedObject.Columns.Count) + return; + var column = this.AssociatedObject.Columns[columnIndex]; + if (column == null) + return; + + List commandParameter = new List(); + commandParameter.Add(this.AssociatedObject); + commandParameter.Add(column); + CommandBinding binding = new CommandBinding(); + this.AssociatedObject.CommandBindings.Add(binding); + + if (columnIndex < 3) + { + e.ContextMenu.Items.Add(new MenuItem() + { + Header = "AddSort", + Command = ContextMenuCommands.AddSort, + CommandParameter = commandParameter + }); + e.ContextMenu.Items.Add(new MenuItem() + { + Header = "AddGroup", + Command = ContextMenuCommands.AddGroup, + CommandParameter = commandParameter + }); + e.ContextMenu.Items.Add(new MenuItem() + { + Header = "ClearSort", + Command = ContextMenuCommands.ClearSort, + CommandParameter = commandParameter + }); + e.ContextMenu.Items.Add(new MenuItem() + { + Header = "ClearGroup", + Command = ContextMenuCommands.ClearGroup, + CommandParameter = commandParameter + }); + } + else + { + e.ContextMenu.Items.Add(new MenuItem() + { + Header = "Copy", + Command = ContextMenuCommands.Copy, + CommandParameter = commandParameter + }); + e.ContextMenu.Items.Add(new MenuItem() + { + Header = "Paste", + Command = ContextMenuCommands.Paste, + CommandParameter = commandParameter + }); + } +} +``` + +![Showing context menu for each column in WPF DataGrid](ShowingContextMenu.png) \ No newline at end of file diff --git a/ShowingContextMenu.png b/ShowingContextMenu.png new file mode 100644 index 0000000..970547e Binary files /dev/null and b/ShowingContextMenu.png differ