|
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 (SfDataGrid) |
| 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?_ga=2.138054064.1225195101.1667794112-766490130.1650530957&_gl=1*jbl2r4*_ga*NzY2NDkwMTMwLjE2NTA1MzA5NTc.*_ga_WC4JKKPHH0*MTY2Nzk4NTI1Mi4yOTMuMS4xNjY3OTkwMjkzLjAuMC4w#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](https://help.syncfusion.com/cr/wpf/Syncfusion.UI.Xaml.Grid.SfDataGrid.html?_ga=2.238060544.1225195101.1667794112-766490130.1650530957&_gl=1*bvr10z*_ga*NzY2NDkwMTMwLjE2NTA1MzA5NTc.*_ga_WC4JKKPHH0*MTY2Nzk4NTI1Mi4yOTMuMS4xNjY3OTkwMzc2LjAuMC4w#Syncfusion_UI_Xaml_Grid_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 | +```c# |
| 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); |
| 14 | + if (columnIndex < 0 && columnIndex >= this.AssociatedObject.Columns.Count) |
| 15 | + return; |
| 16 | + var column = this.AssociatedObject.Columns[columnIndex]; |
| 17 | + if (column == null) |
| 18 | + return; |
| 19 | + |
| 20 | + List<object> commandParameter = new List<object>(); |
| 21 | + commandParameter.Add(this.AssociatedObject); |
| 22 | + commandParameter.Add(column); |
| 23 | + CommandBinding binding = new CommandBinding(); |
| 24 | + this.AssociatedObject.CommandBindings.Add(binding); |
| 25 | + |
| 26 | + if (columnIndex < 3) |
| 27 | + { |
| 28 | + e.ContextMenu.Items.Add(new MenuItem() |
| 29 | + { |
| 30 | + Header = "AddSort", |
| 31 | + Command = ContextMenuCommands.AddSort, |
| 32 | + CommandParameter = commandParameter |
| 33 | + }); |
| 34 | + e.ContextMenu.Items.Add(new MenuItem() |
| 35 | + { |
| 36 | + Header = "AddGroup", |
| 37 | + Command = ContextMenuCommands.AddGroup, |
| 38 | + CommandParameter = commandParameter |
| 39 | + }); |
| 40 | + e.ContextMenu.Items.Add(new MenuItem() |
| 41 | + { |
| 42 | + Header = "ClearSort", |
| 43 | + Command = ContextMenuCommands.ClearSort, |
| 44 | + CommandParameter = commandParameter |
| 45 | + }); |
| 46 | + e.ContextMenu.Items.Add(new MenuItem() |
| 47 | + { |
| 48 | + Header = "ClearGroup", |
| 49 | + Command = ContextMenuCommands.ClearGroup, |
| 50 | + CommandParameter = commandParameter |
| 51 | + }); |
| 52 | + } |
| 53 | + else |
| 54 | + { |
| 55 | + e.ContextMenu.Items.Add(new MenuItem() |
| 56 | + { |
| 57 | + Header = "Copy", |
| 58 | + Command = ContextMenuCommands.Copy, |
| 59 | + CommandParameter = commandParameter |
| 60 | + }); |
| 61 | + e.ContextMenu.Items.Add(new MenuItem() |
| 62 | + { |
| 63 | + Header = "Paste", |
| 64 | + Command = ContextMenuCommands.Paste, |
| 65 | + CommandParameter = commandParameter |
| 66 | + }); |
| 67 | + } |
| 68 | +} |
| 69 | +``` |
6 | 70 |
|
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) |
| 71 | + |
0 commit comments