|
1 | | -# How to show different context menu for each column in WPF DataGrid (SfDataGrid)? |
| 1 | +# How to Show Different Context Menu for each Column in WPF DataGrid? |
| 2 | + |
| 3 | +[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. |
2 | 4 |
|
3 | | -This example illustrates how to show different context menu for each column in [WPF DataGrid](https://www.syncfusion.com/wpf-ui-controls/datagrid) (SfDataGrid). |
| 5 | +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, |
4 | 6 |
|
5 | | -[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. |
| 7 | +```csharp |
| 8 | +this.AssociatedObject.GridContextMenuOpening += AssociatedObject_GridContextMenuOpening; |
| 9 | + |
| 10 | +private void AssociatedObject_GridContextMenuOpening(object sender, GridContextMenuEventArgs e) |
| 11 | +{ |
| 12 | + e.ContextMenu.Items.Clear(); |
| 13 | + var columnIndex = this.AssociatedObject.ResolveToGridVisibleColumnIndex(e.RowColumnIndex.ColumnIndex); |
6 | 14 |
|
7 | | -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) |
| 15 | + if (columnIndex < 0 && columnIndex >= this.AssociatedObject.Columns.Count) |
| 16 | + return; |
| 17 | + var column = this.AssociatedObject.Columns[columnIndex]; |
| 18 | + if (column == null) |
| 19 | + return; |
| 20 | + |
| 21 | + List<object> commandParameter = new List<object>(); |
| 22 | + commandParameter.Add(this.AssociatedObject); |
| 23 | + commandParameter.Add(column); |
| 24 | + CommandBinding binding = new CommandBinding(); |
| 25 | + this.AssociatedObject.CommandBindings.Add(binding); |
| 26 | + |
| 27 | + if (columnIndex < 3) |
| 28 | + { |
| 29 | + e.ContextMenu.Items.Add(new MenuItem() |
| 30 | + { |
| 31 | + Header = "AddSort", |
| 32 | + Command = ContextMenuCommands.AddSort, |
| 33 | + CommandParameter = commandParameter |
| 34 | + }); |
| 35 | + e.ContextMenu.Items.Add(new MenuItem() |
| 36 | + { |
| 37 | + Header = "AddGroup", |
| 38 | + Command = ContextMenuCommands.AddGroup, |
| 39 | + CommandParameter = commandParameter |
| 40 | + }); |
| 41 | + e.ContextMenu.Items.Add(new MenuItem() |
| 42 | + { |
| 43 | + Header = "ClearSort", |
| 44 | + Command = ContextMenuCommands.ClearSort, |
| 45 | + CommandParameter = commandParameter |
| 46 | + }); |
| 47 | + e.ContextMenu.Items.Add(new MenuItem() |
| 48 | + { |
| 49 | + Header = "ClearGroup", |
| 50 | + Command = ContextMenuCommands.ClearGroup, |
| 51 | + CommandParameter = commandParameter |
| 52 | + }); |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + e.ContextMenu.Items.Add(new MenuItem() |
| 57 | + { |
| 58 | + Header = "Copy", |
| 59 | + Command = ContextMenuCommands.Copy, |
| 60 | + CommandParameter = commandParameter |
| 61 | + }); |
| 62 | + e.ContextMenu.Items.Add(new MenuItem() |
| 63 | + { |
| 64 | + Header = "Paste", |
| 65 | + Command = ContextMenuCommands.Paste, |
| 66 | + CommandParameter = commandParameter |
| 67 | + }); |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | + |
0 commit comments