|
7 | 7 |
|
8 | 8 | # Blazor Grid - Customize Context Menu |
9 | 9 |
|
10 | | -This repository displays for various DxGrid areas, customizes their items using DxGrid's built-in APIs, and defines context menus for areas that do not include built-in context menus. |
| 10 | +This example shows how to enable, customize, and implement context menus in Blazor Grid and Toolbar components. It demonstrates the following tasks: |
| 11 | +* Enable built-in context menus for DxGrid's predefined areas: header, footer, data row, group panel, group row, and group footer. |
| 12 | +* Customize context menu items for header, footer, and data rows using DxGrid APIs. |
| 13 | +* Define custom context menus for areas that do not include built-in context menus. |
11 | 14 |
|
12 | 15 |  |
13 | 16 |
|
14 | 17 | ## Implementation Details |
15 | 18 |
|
16 | | -### Display Context Menus for All Built-in Areas |
| 19 | +### Enable Context Menus for All Predefined Areas |
17 | 20 |
|
18 | | -Set the [ContextMenus](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.ContextMenus) property to `All` to enable DxGrid built-in context menus for the following areas: Header, Footer, Data Row, Group Panel, Group Row, and Group Footer. |
| 21 | +Set the [ContextMenus](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.ContextMenus) property to `All` to allow users to use built-in context menus in the following Grid areas: header, footer, data row, group panel, group row, and group footer. |
19 | 22 |
|
20 | | -File to Review: [Index.razor](./CS/GridWithContextMenu/Pages/Index.razor#L14) |
| 23 | +``` |
| 24 | +<DxGrid @ref="Grid" |
| 25 | + ... |
| 26 | + ContextMenus="GridContextMenus.All"> |
| 27 | +</DxGrid> |
| 28 | +``` |
21 | 29 |
|
22 | | -### Customize Context Menu Items Using Grid's Built-in API |
| 30 | +### Customize Context Menu Items Using Grid APIs |
23 | 31 |
|
24 | | -DxGrid Component includes built-in APIs to customize context menu items. Follow the steps below to implement them: |
| 32 | +The `DxGrid` component includes built-in APIs used to customize context menu items. This scenario affects the following Grid areas: header, footer, and data rows. |
25 | 33 |
|
26 | | -1. Add the [oncontextmenu:preventDefault](./CS/GridWithContextMenu/Pages/Index.razor#L14) to disable the standard browser context menu. |
27 | | -2. In the Grid's [CustomizeContextMenu](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeContextMenu) event handler, subscribe to the **contextmenu** event to display the custom context menu items. |
| 34 | +In the Grid's [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 the built-in context menu items. |
28 | 35 |
|
29 | | -This scenario affects the following DxGrid areas: Header, Footer, and Data Rows. |
| 36 | +``` |
| 37 | +<DxGrid @ref="Grid" |
| 38 | + CustomizeContextMenu="Grid_CustomizeContextMenu" |
| 39 | + ... > |
| 40 | + <!-- ... --> |
| 41 | +</DxGrid> |
30 | 42 |
|
31 | | -Files to Review: |
32 | | -* [Index.razor](./CS/GridWithContextMenu/Pages/Index.razor#L64) |
33 | | -* [GridContextMenuHelper.cs](./CS/GridWithContextMenu/Data/GridContextMenuHelper.cs#L298-L365) |
| 43 | +@code { |
| 44 | + IGrid Grid { get; set; } |
| 45 | + // ... |
34 | 46 |
|
35 | | -### Add Custom Context Menus |
36 | | - |
37 | | -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. Follow the steps below to display custom context menus: |
| 47 | + void Grid_CustomizeContextMenu(GridCustomizeContextMenuEventArgs args) => GridContextMenuHelper.CustomizeContextMenu(args); |
| 48 | +} |
| 49 | +``` |
38 | 50 |
|
39 | | -1. Add the [oncontextmenu:preventDefault](./CS/GridWithContextMenu/Pages/Index.razor#L14) to disable the standard browser context menu. |
40 | | -2. In the Grid's [CustomizeElement](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeElement) event handler, subscribe to the **contextmenu** event to display the custom Context Menu. |
41 | | - |
42 | | -This scenario affects the following application areas: Toolbar Component, DxGrid's Edit Row, and DxGrid's Pager. |
| 51 | +### Add Custom Context Menus |
43 | 52 |
|
| 53 | +Follow the steps below to display custom context menus for the toolbar component, Grid's edit row, and Grid's pager: |
| 54 | + |
| 55 | +1. In the [GridContextMenuContainer](./CS/GridWithContextMenu/Pages/GridContextMenuContainer.razor#L4-L28) component, define the context menu components. |
| 56 | +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) uses them to execute commands. |
| 57 | +3. Add the [oncontextmenu:preventDefault](./CS/GridWithContextMenu/Pages/Index.razor#L14) directive to disable the standard browser context menu. |
| 58 | +4. In the Grid's [CustomizeElement](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeElement) event handler, subscribe to the **contextmenu** event to display the custom context menu. |
| 59 | + |
| 60 | +``` |
| 61 | +<DxGrid @ref="Grid" |
| 62 | + ... |
| 63 | + CustomizeElement="Grid_CustomizeElement" |
| 64 | + @oncontextmenu:preventDefault> |
| 65 | + <!-- ... --> |
| 66 | +</DxGrid> |
| 67 | +
|
| 68 | +<GridContextMenuContainer Grid="Grid" @ref="ContextMenuContainer" /> |
| 69 | +
|
| 70 | +@code { |
| 71 | + IGrid Grid { get; set; } |
| 72 | + GridContextMenuContainer ContextMenuContainer { get; set; } |
| 73 | + // ... |
| 74 | +
|
| 75 | + void Grid_CustomizeElement(GridCustomizeElementEventArgs e) { |
| 76 | + if(GridContextMenuHelper.IsContextMenuElement(e.ElementType)) { |
| 77 | + e.Attributes["oncontextmenu"] = EventCallback.Factory.Create<MouseEventArgs>( |
| 78 | + this, |
| 79 | + async mArgs => await ContextMenuContainer.Grid_ContextMenu(e, mArgs) |
| 80 | + ); |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | +``` |
| 85 | + |
| 86 | +### Video |
44 | 87 |
|
45 | | -Files to Review: |
46 | | -* [Index.razor](./CS/GridWithContextMenu/Pages/Index.razor#L66) |
47 | | -* [GridContextMenuContainer.razor](./CS/GridWithContextMenu/Pages/GridContextMenuContainer.razor#L4-L28) |
48 | | -* [GridContextMenuHelper.cs](./CS/GridWithContextMenu/Data/GridContextMenuHelper.cs#L31-59) |
| 88 | +- [Adding a Context Menu to a Grid](https://www.youtube.com/watch?v=TfBR77ARnf8&t) |
49 | 89 |
|
50 | | -<!-- |
51 | 90 | ## Files to Review |
52 | 91 |
|
53 | 92 | - [Index.razor](./CS/GridWithContextMenu/Pages/Index.razor) |
54 | 93 | - [GridContextMenuHelper.cs](./CS/GridWithContextMenu/Data/GridContextMenuHelper.cs) |
55 | | -- [GridContextMenuContainer.razor](./CS/GridWithContextMenu/Pages/GridContextMenuContainer.razor) --> |
| 94 | +- [GridContextMenuContainer.razor](./CS/GridWithContextMenu/Pages/GridContextMenuContainer.razor) |
56 | 95 |
|
57 | 96 | ## Documentation |
58 | 97 |
|
59 | 98 | - [DxContextMenu](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxContextMenu) |
60 | 99 | - [DxGrid](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid) |
61 | | - |
62 | | -## Video |
63 | | - |
64 | | -- [Adding a Context Menu to a Grid](https://www.youtube.com/watch?v=TfBR77ARnf8&t) |
| 100 | +- [DxGrid.CustomizeElement](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeElement) |
| 101 | +- [DxGrid.CustomizeContextMenu](https://docs.devexpress.com/Blazor/DevExpress.Blazor.DxGrid.CustomizeContextMenu) |
65 | 102 |
|
66 | 103 | ## More Examples |
67 | 104 |
|
|
0 commit comments