Skip to content

Commit dbfeaac

Browse files
ES-975464 - Resolve the ReadMe file length issue in this sample repository
1 parent d6a00d2 commit dbfeaac

File tree

1 file changed

+68
-4
lines changed

1 file changed

+68
-4
lines changed

README.md

Lines changed: 68 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,71 @@
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.
24

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`,
46

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+
```
670

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+
![context menu for each column in WPF DataGrid](https://www.syncfusion.com/uploads/user/kb/wpf/wpf-44389/wpf-44389_img1.png)

0 commit comments

Comments
 (0)