Skip to content

Commit b53e1b0

Browse files
ES-975464 - Addressed the concern
1 parent 78c38fc commit b53e1b0

File tree

2 files changed

+60
-59
lines changed

2 files changed

+60
-59
lines changed

README.md

Lines changed: 60 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -2,70 +2,71 @@
22

33
[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.
44

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,
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,
66

7-
```c#
7+
```csharp
88
this.AssociatedObject.GridContextMenuOpening += AssociatedObject_GridContextMenuOpening;
99

1010
private void AssociatedObject_GridContextMenuOpening(object sender, GridContextMenuEventArgs e)
1111
{
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-
}
12+
e.ContextMenu.Items.Clear();
13+
var columnIndex = this.AssociatedObject.ResolveToGridVisibleColumnIndex(e.RowColumnIndex.ColumnIndex);
14+
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+
}
6869
}
6970
```
7071

71-
![context menu for each column in WPF DataGrid](https://www.syncfusion.com/uploads/user/kb/wpf/wpf-44389/wpf-44389_img1.png)
72+
![Showing context menu for each column in WPF DataGrid](ShowingContextMenu.png)

ShowingContextMenu.png

78.1 KB
Loading

0 commit comments

Comments
 (0)