Skip to content

Commit b6eab5b

Browse files
Merge pull request #13 from DevExpress-Examples/context-menu-update
Context menu - Readme update
2 parents 6f0bdba + 464e501 commit b6eab5b

3 files changed

Lines changed: 85 additions & 9 deletions

File tree

CS/GridWithContextMenu/Pages/Index.razor

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@
1313
DataItemDeleting="Grid_DataItemDeleting"
1414
ContextMenus="GridContextMenus.All"
1515
CustomizeContextMenu="Grid_CustomizeContextMenu"
16-
@oncontextmenu:preventDefault>
16+
@oncontextmenu:preventDefault
17+
ShowGroupPanel="true">
1718
<ToolbarTemplate>
1819
<DxToolbar ItemRenderStyleMode="ToolbarRenderStyleMode.Plain">
1920
<DxToolbarItem Text="New" Click="NewItem_Click" />

README.md

Lines changed: 83 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,93 @@
44
[![](https://img.shields.io/badge/📖_How_to_use_DevExpress_Examples-e9f6fc?style=flat-square)](https://docs.devexpress.com/GeneralInformation/403183)
55
[![](https://img.shields.io/badge/💬_Leave_Feedback-feecdd?style=flat-square)](#does-this-example-address-your-development-requirementsobjectives)
66
<!-- default badges end -->
7+
# Blazor Grid - Customize Context Menu
78

8-
# Blazor Grid - Display a Context Menu
9+
This example enables and customizes context menus within both the DevExpress Blazor Grid and Toolbar component. It illustrates use of the following capabilities:
910

10-
You can display a Context Menu when you right-click any Blazor Grid element. In this example, a click on a column header or row invokes the Context Menu.
11+
* Activates built-in context menus for predefined Grid regions including: header, footer, data row, group panel, group row, and group footer.
12+
* Customizes context menu items for header, footer, and data rows using Grid APIs.
13+
* Defines custom context menus for regions that do not include built-in context menus.
1114

1215
![Grid with Context Menu for a column](result.png)
1316

14-
Add the **oncontextmenu:preventDefault** to disable the standard browser context menu. In the Grid's [CustomizeElement](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeElement) event handler, subscribe to the **contextmenu** event (displays the custom Context Menu).
17+
## Implementation Details
1518

16-
The [GridContextMenuContainer](./CS/GridWithContextMenu/Pages/GridContextMenuContainer.razor) component contains Context Menu components. The [GridContextMenuHelper](./CS/GridWithContextMenu/Data/GridContextMenuHelper.cs) class implements Context Menu's item generation, manages state, and includes the click handlers that use the Blazor [Grid's API](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid._methods) to execute commands.
19+
### Enable Context Menus for All Predefined Areas
20+
21+
Set the [ContextMenus](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.ContextMenus) property to `All` to activate use of built-in context menus in the following Grid regions: header, footer, data row, group panel, group row, and group footer.
22+
23+
```
24+
<DxGrid @ref="Grid"
25+
...
26+
ContextMenus="GridContextMenus.All">
27+
</DxGrid>
28+
```
29+
30+
### Customize Context Menu Items Using Grid APIs
31+
32+
`DxGrid` uses built-in APIs to add the following context menu items:
33+
34+
* Save, Cancel, New, Edit, and Delete items to data rows.
35+
* Filter Row, Clear Filter, Footer (checkbox), Fix Column to the Left, Fix Column to the Right, and Unfix Column to the header.
36+
* Footer (checkbox) to the footer.
37+
38+
In the [CustomizeContextMenu](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeContextMenu) event handler, call the helper method defined in the [GridContextMenuHelper.cs](./CS/GridWithContextMenu/Data/GridContextMenuHelper.cs#L298-L365) class to customize built-in context menu items.
39+
40+
```
41+
<DxGrid @ref="Grid"
42+
CustomizeContextMenu="Grid_CustomizeContextMenu"
43+
... >
44+
<!-- ... -->
45+
</DxGrid>
46+
47+
@code {
48+
IGrid Grid { get; set; }
49+
// ...
50+
51+
void Grid_CustomizeContextMenu(GridCustomizeContextMenuEventArgs args) => GridContextMenuHelper.CustomizeContextMenu(args);
52+
}
53+
```
54+
55+
### Add Custom Context Menus
56+
57+
To display custom context menus for the toolbar component, Grid's edit row and pager, you must:
58+
59+
1. In the [GridContextMenuContainer](./CS/GridWithContextMenu/Pages/GridContextMenuContainer.razor#L4-L28) component, define context menu components.
60+
2. In the [GridContextMenuHelper](./CS/GridWithContextMenu/Data/GridContextMenuHelper.cs#L31-L59) class, implement context menu item generation, state management, and click handlers. The Blazor [Grid API](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid._methods) calls these helper methods to execute commands.
61+
3. Add the [oncontextmenu:preventDefault](./CS/GridWithContextMenu/Pages/Index.razor#L14) directive to disable the standard browser context menu.
62+
4. In the [CustomizeElement](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeElement) event handler, subscribe to the **contextmenu** event to display a custom context menu.
63+
64+
65+
```
66+
<DxGrid @ref="Grid"
67+
...
68+
CustomizeElement="Grid_CustomizeElement"
69+
@oncontextmenu:preventDefault>
70+
@* ... *@
71+
</DxGrid>
72+
73+
<GridContextMenuContainer Grid="Grid" @ref="ContextMenuContainer" />
74+
75+
@code {
76+
IGrid Grid { get; set; }
77+
GridContextMenuContainer ContextMenuContainer { get; set; }
78+
// ...
79+
80+
void Grid_CustomizeElement(GridCustomizeElementEventArgs e) {
81+
if(GridContextMenuHelper.IsContextMenuElement(e.ElementType)) {
82+
e.Attributes["oncontextmenu"] = EventCallback.Factory.Create<MouseEventArgs>(
83+
this,
84+
async mArgs => await ContextMenuContainer.Grid_ContextMenu(e, mArgs)
85+
);
86+
}
87+
}
88+
}
89+
```
90+
91+
#### Video
92+
93+
- [Adding a Context Menu to a Grid](https://www.youtube.com/watch?v=TfBR77ARnf8&t)
1794

1895
## Files to Review
1996

@@ -25,10 +102,8 @@ The [GridContextMenuContainer](./CS/GridWithContextMenu/Pages/GridContextMenuCon
25102

26103
- [DxContextMenu](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxContextMenu)
27104
- [DxGrid](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid)
28-
29-
## Video
30-
31-
- [Adding a Context Menu to a Grid](https://www.youtube.com/watch?v=TfBR77ARnf8&t)
105+
- [DxGrid.CustomizeElement](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeElement)
106+
- [DxGrid.CustomizeContextMenu](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeContextMenu)
32107

33108
## More Examples
34109

result.png

26 KB
Loading

0 commit comments

Comments
 (0)