blazor-devexpress-dot-blazor-e4c68088.md
An adaptive menu component.
Namespace : DevExpress.Blazor
Assembly : DevExpress.Blazor.v25.2.dll
NuGet Package : DevExpress.Blazor
public class DxMenu :
DxComponent,
IRequireSelfCascading,
IModelWrapper<IMenuModel>
<DxMenu> adds a menu to your Blazor application.
To add a <DxMenu> component to an application, follow the steps below:
<DxMenu>…</DxMenu> markup to your application.Refer to the following list for the component API reference: DxMenu Members.
In static render mode, you can use a single-level menu.
<DxMenu Orientation="@Orientation.Vertical">
<Items>
<DxMenuItem NavigateUrl="/" Text="Home" />
<DxMenuItem NavigateUrl="/counter" Text="Counter" />
<DxMenuItem NavigateUrl="/weather" Text="Weather" />
</Items>
</DxMenu>
If you need interactivity (for example, to support adaptivity), enable interactive render mode. Refer to the following topic for more details: Enable Interactive Render Mode.
Specify menu items in the DxMenu.Items collection. Each item is a DxMenuItem class instance that can have a collection of child items (the DxMenuItem.Items property).
Use the ItemsPosition property to align items in the menu. To customize item content and appearance, use the Text, IconCssClass, DxMenu.SubMenuCssClass, and DxMenuItem.SubMenuCssClass properties. To add a separator before an item, use the BeginGroup property.
You can also use the Expanded property to show a drop-down window with child items. Note that a node is visible only when all its parent nodes are visible.
<div class="card">
<DxMenu Title="DevExpress"
ItemsPosition="ItemPosition.Start">
<Items>
<DxMenuItem Text="Products" IconCssClass="oi oi-layers" Expanded="true">
<Items>
<DxMenuItem Text="Subscriptions / Packs" />
<DxMenuItem Text=".NET Windows Forms Components" />
<DxMenuItem Text="Reporting / Printing Suites" />
<DxMenuItem Text="VCL Components and Tools" />
<DxMenuItem Text="ASP.NET Components" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Support" IconCssClass="oi oi-person">
<Items>
<DxMenuItem Text="Knowledge Base" />
<DxMenuItem Text="Documentation" />
<DxMenuItem Text="Support Center" />
<DxMenuItem Text="Newsgroups" />
<DxMenuItem Text="Best Practices" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Documentation" IconCssClass="oi oi-book" BeginGroup="true" />
<DxMenuItem Text="Demos" IconCssClass="oi oi-monitor" />
<DxMenuItem Text="Blogs" IconCssClass="oi oi-bell" />
</Items>
</DxMenu>
</div>
You can populate the Menu component with items from a data source.
Follow the steps below to bind Menu to data:
Use the Data property to specify a data source. You can use different collection types:
Add the DataMappings tag to the component’s markup.
Create the DxMenuDataMapping instance and map item properties (BeginGroup, CssClass, and so on) to data source fields. Mappings are used to assign data from the source collection to the component’s data model.
The following code snippet loads menu items from the TextFormattingMenu class. Each menu item has child items. The code specifies the Children and Text mappings to adjust the menu data model to the specified data source.
<div class="card menu-card-container w-100">
<div class="card-header p-0 fw-normal">
<DxMenu ItemClick="@(e => ((TextFormattingMenuItem)e.ItemInfo.Data).Click())"
Data="@MenuItems"
SizeMode="@Params.SizeMode">
<DataMappings>
<DxMenuDataMapping Text="Text"
Children="Children"
BeginGroup="BeginGroup"
CssClass="CssClass"
Enabled="Enabled" />
</DataMappings>
</DxMenu>
</div>
<div class="card-body">
<div class="menu-card-text-container">
<span class="demo-preventsel">Lorem ipsum dolor sit amet, consectetur adipiscing elit. Mauris sit amet metus vel nisi blandit tincidunt vel efficitur purus. Nunc nec turpis tempus, accumsan orci auctor, imperdiet mauris. Fusce id purus magna.</span>
</div>
</div>
</div>
@code {
List<TextFormattingMenuItem> menuItems;
List<TextFormattingMenuItem> MenuItems => menuItems = menuItems ?? TextFormattingMenu.MenuItems(Formatting);
TextFormatting Formatting { get; } = new TextFormatting();
}
using System.Collections.Generic;
using System.Linq;
namespace BlazorDemo.Data {
public class TextFormatting {
public Dictionary<string, bool> Decoration { get; } = new Dictionary<string, bool>() {
{ "Bold", false },
{ "Italic" , false },
{ "Underline" , false },
{ "Strikethrough" , false },
{ "Overline" , false }
};
public string FontFamily { get; set; }
public int FontSize { get; set; }
public string TextCase { get; set; }
string GetTextDecoration() {
string textDecoration = "";
if(Decoration["Underline"])
textDecoration += "underline";
if(Decoration["Overline"])
textDecoration += " overline";
if(Decoration["Strikethrough"])
textDecoration += " line-through";
return textDecoration;
}
public string GetStyleString() {
string style = "";
if(!string.IsNullOrEmpty(FontFamily))
style += $"font-family: {FontFamily}; ";
if(FontSize > 0)
style += $"font-size: {FontSize}pt; ";
if(Decoration["Bold"])
style += "font-weight: bold; ";
if(Decoration["Italic"])
style += "font-style: italic; ";
if(!string.IsNullOrEmpty(TextCase))
style += $"text-transform: {TextCase}; ";
string textDecoration = GetTextDecoration();
if(!string.IsNullOrEmpty(textDecoration))
style += $"text-decoration: {textDecoration};";
return !string.IsNullOrEmpty(style) ? style : null;
}
public bool IsStyleChanged() {
return TextCase != null || FontFamily != null || FontSize != 0 || Decoration.Values.Any(x => x);
}
public void ClearFormatting() {
TextCase = null;
FontFamily = null;
FontSize = 0;
Decoration["Bold"] = false;
Decoration["Italic"] = false;
Decoration["Underline"] = false;
Decoration["Overline"] = false;
Decoration["Strikethrough"] = false;
}
}
}
using System.Collections.Generic;
namespace BlazorDemo.Data {
static class TextFormattingMenu {
public static TextFormattingMenuItem FontFamilyMenuItem(TextFormatting textFormatting) =>
new TextFormattingParentMenuItem(textFormatting, "Font", new List<TextFormattingMenuItem>() {
new FontFamilyMenuItem(textFormatting, "Times New Roman", "Times New Roman"),
new FontFamilyMenuItem(textFormatting, "Tahoma", "Tahoma"),
new FontFamilyMenuItem(textFormatting, "Verdana", "Verdana"),
new FontFamilyMenuItem(textFormatting, "Arial", "Arial"),
new FontFamilyMenuItem(textFormatting, "MS Sans Serif", "MS Sans Serif"),
new FontFamilyMenuItem(textFormatting, "Courier", "Courier"),
new FontFamilyMenuItem(textFormatting, "Segoe UI", "Segoe UI"),
new FontFamilyMenuItem(textFormatting, "Default", null) { BeginGroup = true, IconCss = "menu-icon menu-icon-check" }
});
public static TextFormattingMenuItem FontSizeMenuItem(TextFormatting textFormatting) =>
new TextFormattingParentMenuItem(textFormatting, "Size", new List<TextFormattingMenuItem>() {
new FontSizeMenuItem(textFormatting, "8pt", 8),
new FontSizeMenuItem(textFormatting, "10pt", 10),
new FontSizeMenuItem(textFormatting, "12pt", 12),
new FontSizeMenuItem(textFormatting, "14pt", 14),
new FontSizeMenuItem(textFormatting, "18pt", 18),
new FontSizeMenuItem(textFormatting, "24pt", 24),
new FontSizeMenuItem(textFormatting, "36pt", 36),
new FontSizeMenuItem(textFormatting, "Default", 0) { BeginGroup = true }
});
// ...
public static List<TextFormattingMenuItem> MenuItems(TextFormatting textFormatting) =>
new List<TextFormattingMenuItem>() {
FontFamilyMenuItem(textFormatting),
FontSizeMenuItem(textFormatting),
new ClearFormattingMenuItem(textFormatting) { BeginGroup = false }
};
}
}
The Menu’s default orientation is Horizontal (menu items are arranged in a row). Use the Orientation property to change the orientation.
<div>
<DxMenu Title="DevExpress"
Orientation="Orientation.Vertical"
DisplayMode="MenuDisplayMode.Desktop">
<Items>
<DxMenuItem Text="Home" IconCssClass="menu-icon-home menu-icon">
<Items>
<DxMenuItem Text="News">
<Items>
<DxMenuItem Text="Explore our newest features" />
<DxMenuItem Text="Website news" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Our Mission" />
<DxMenuItem Text="Our Customers" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Components" IconCssClass="menu-icon-products menu-icon">
<Items>
<DxMenuItem Text="Blazor" />
<DxMenuItem Text="ASP.NET MVC" />
<DxMenuItem Text="ASP.NET Web Forms" />
<DxMenuItem Text="ASP.NET Core" />
<DxMenuItem Text="Bootstrap Web Forms" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Support" IconCssClass="menu-icon-support menu-icon">
<Items>
<DxMenuItem Text="Getting Started" />
<DxMenuItem Text="Documentation" />
<DxMenuItem Text="Support Center" />
<DxMenuItem Text="Code Examples" />
<DxMenuItem Text="Blogs" />
</Items>
</DxMenuItem>
</Items>
</DxMenu>
</div>
The Menu component supports different display modes (the DisplayMode property):
Auto - the menu is adapted to the device type.
Desktop - the menu is shown as a panel with root items. The menu view also depends on the orientation. In the horizontal orientation, child items are available in drop-down menus. In the vertical orientation, submenus with child items are shown to the side of the menu container.
Mobile - the menu has a compact view and depends on the orientation. In the horizontal orientation, it is a hamburger menu. In the vertical orientation, the menu looks like a desktop menu - a panel with root items, but submenus are shown in the main menu container and have the Back button.
The following code snippet specifies display mode:
<div>
<DxMenu Title="DevExpress"
Orientation="Orientation.Vertical"
DisplayMode="MenuDisplayMode.Mobile">
<Items>
<DxMenuItem Text="Home" IconCssClass="menu-icon-home menu-icon">
<Items>
<DxMenuItem Text="News">
<Items>
<DxMenuItem Text="Explore our newest features" />
<DxMenuItem Text="Website news" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Our Mission" />
<DxMenuItem Text="Our Customers" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Components" IconCssClass="menu-icon-products menu-icon">
<Items>
<DxMenuItem Text="Blazor" />
<DxMenuItem Text="ASP.NET MVC" />
<DxMenuItem Text="ASP.NET Web Forms" />
<DxMenuItem Text="ASP.NET Core" />
<DxMenuItem Text="Bootstrap Web Forms" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Support" IconCssClass="menu-icon-support menu-icon">
<Items>
<DxMenuItem Text="Getting Started" />
<DxMenuItem Text="Documentation" />
<DxMenuItem Text="Support Center" />
<DxMenuItem Text="Code Examples" />
<DxMenuItem Text="Blogs" />
</Items>
</DxMenuItem>
</Items>
</DxMenu>
</div>
The Menu component supports adaptive mode when items are arranged horizontally. The following properties specify how the menu behaves when the width of the browser window changes and it cannot fit all items:
CollapseItemToIconModeSpecifies whether the menu displays icons instead of captions in items. You can also specify the order in which menu hides its items (AdaptivePriority).CollapseItemsToHamburgerMenuSpecifies whether menu items are collapsed into a hamburger menu. Use the HamburgerButtonPosition property to specify the hamburger button’s position.
<div class="card w-auto">
<DxMenu Title="DevExpress"
CollapseItemToIconMode="MenuCollapseItemToIconMode.Sequentially"
CollapseItemsToHamburgerMenu="true">
<Items>
<DxMenuItem Text="Home" IconCssClass="menu-icon-home menu-icon">
<Items>
<DxMenuItem Text="News">
<Items>
<DxMenuItem Text="Explore our newest features" />
<DxMenuItem Text="Website news" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Our Mission" />
<DxMenuItem Text="Our Customers" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Components" IconCssClass="menu-icon-products menu-icon">
<Items>
<DxMenuItem Text="Blazor" />
<DxMenuItem Text="ASP.NET MVC" />
<DxMenuItem Text="ASP.NET Web Forms" />
<DxMenuItem Text="ASP.NET Core" />
<DxMenuItem Text="Bootstrap Web Forms" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Support" IconCssClass="menu-icon-support menu-icon">
<Items>
<DxMenuItem Text="Getting Started" />
<DxMenuItem Text="Documentation" />
<DxMenuItem Text="Support Center" />
<DxMenuItem Text="Code Examples" />
<DxMenuItem Text="Blogs" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Contacts" IconCssClass="menu-icon-contacts menu-icon" />
<DxMenuItem Text="About" IconCssClass="menu-icon-about menu-icon" />
</Items>
</DxMenu>
</div>
You can also use the DisplayMode property in Auto mode to show the Menu as a panel or in a compact view depending on the device type.
The Menu component allows you to use templates to customize the layout and appearance of the menu title and menu items.
Use the TitleTemplate property to specify the template for the menu title.
<div class="card w-50">
<DxMenu Title="DevExpress Logo">
<TitleTemplate>
</TitleTemplate>
<Items>
<DxMenuItem Text="Products" />
<DxMenuItem Text="Support" />
<DxMenuItem Text="Documentation" />
<DxMenuItem Text="Demos" />
</Items>
</DxMenu>
</div>
You can specify a template for all items or for an individual item. Individual templates override common templates.
| What to Customize | Common Templates | Individual Templates |
|---|---|---|
| A whole item | DxMenu.ItemTemplate | DxMenuItem.Template |
| An item’s text | DxMenu.ItemTextTemplate | DxMenuItem.TextTemplate |
| An item’s submenu | DxMenu.SubMenuTemplate | DxMenuItem.SubMenuTemplate |
The following example uses templates to create the Search and User Profile menu items:
<DxMenu Orientation="Orientation.Horizontal"
@* ... *@
SubMenuCssClass="menu-dropdown"
SizeMode="@Params.SizeMode">
<TitleTemplate>
<div class="icon-logo" role="img" aria-label="@context"></div>
</TitleTemplate>
<Items>
<DxMenuItem Text="Home" IconCssClass="menu-icon-home menu-icon">
<Items>
<DxMenuItem Text="News">
<Items>
<DxMenuItem Text="Explore our newest features" />
<DxMenuItem Text="Website news" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Our Mission" />
<DxMenuItem Text="Our Customers" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Components" IconCssClass="menu-icon-products menu-icon">
<Items>
<DxMenuItem Text="Blazor" />
<DxMenuItem Text="ASP.NET MVC" />
<DxMenuItem Text="ASP.NET Web Forms" />
<DxMenuItem Text="ASP.NET Core" />
<DxMenuItem Text="Bootstrap Web Forms" />
</Items>
</DxMenuItem>
<DxMenuItem Text="Support" IconCssClass="menu-icon-support menu-icon">
<Items>
<DxMenuItem Text="Getting Started" />
<DxMenuItem Text="Documentation" />
<DxMenuItem Text="Support Center" />
<DxMenuItem Text="Code Examples" />
<DxMenuItem Text="Blogs" />
</Items>
</DxMenuItem>
<DxMenuItem CssClass="search-menu-item" Position="ItemPosition.End">
<Template>
<DxSearchBox CssClass="search" aria-label="Search" />
</Template>
</DxMenuItem>
<DxMenuItem CssClass="notoggle" Position="ItemPosition.End">
<TextTemplate>
<div class="menu-icon-person-circle menu-icon" />
</TextTemplate>
<SubMenuTemplate>
<div class="w-100 user-profile">
<div class="flex-column align-items-center justify-content-center">
<div class="logo-container d-flex align-items-center justify-content-center">
<div class="menu-icon-person-circle menu-icon menu-icon-large" />
</div>
<div class="user-name-container bm-3 mb-2">
<div class="tm-8 text-center">User Name</div>
<div class="text-center">John Heart</div>
</div>
<div class="d-flex justify-content-center log-off-btn">
<DxButton Text="Log Off" RenderStyle="@ButtonRenderStyle.Secondary"></DxButton>
</div>
</div>
</div>
</SubMenuTemplate>
</DxMenuItem>
</Items>
</DxMenu>
.dxbl-menu-item .search {
min-width: 123px
}
.dxbl-menu-item-tmpl > .search {
position: relative;
}
.user-profile .logo-container {
margin-top: 1.25rem;
margin-bottom: 1.25rem;
}
.user-profile .log-off-btn {
padding-top: 0.815rem;
padding-bottom: 0.815rem;
border-top: 1px solid #e5e5e5;
}
.menu-icon-user-profile {
opacity: 0.75;
}
.user-profile .menu-icon-user-profile {
opacity: 0.25;
margin-top: 1.25rem;
margin-bottom: 1.25rem;
}
.user-name-container .tm-8 {
opacity: 0.8;
}
The DevExpress Blazor Menu component supports keyboard shortcuts that allow users to access every UI element and navigate through menu and submenu items. Keyboard navigation is implemented both on the client and server.
Note
Keyboard support allows users to interact with application content in cases they cannot use a mouse or they rely on assistive technologies (like screen readers or switch devices). Refer to the Accessibility help topic for information on other accessibility areas that we address.
The table below lists common keyboard shortcuts:
| Shortcut Keys | Description |
|---|---|
| Tab, Shift + Tab | Focuses the menu/hamburger button or moves focus to the next or previous focusable page element. |
| Enter, Space | For items without child items: Invokes a click event handler for the focused menu item, closes submenus, and moves focus to the corresponding main menu item. |
| For items with child items: Invokes a click event handler for the focused menu item, opens a submenu, and moves focus to its first item. For templated items: Moves focus to the first focusable element within the template. | |
| Esc, Tab | For templates within menu items: Moves focus to the corresponding menu item. |
The following sections describe keyboard shortcuts available in different menu display and orientation modes.
| Shortcut Keys | Description |
|---|---|
| End | Moves focus to the last menu/submenu item. |
| Home | Moves focus to the first menu/submenu item. |
| Esc | Closes opened submenus and moves focus to the corresponding main menu item. |
| Up Arrow | For main menu items with child items: Opens a submenu and moves focus to its last item. |
| For submenu items: Moves focus to the previous item. | |
| Down Arrow | For main menu items with child items: Opens a submenu and moves focus to its first item. |
| For submenu items: Moves focus to the next item. | |
| Right Arrow | For main menu items: Moves focus to the next item. |
| For submenu items with child items: Opens a submenu and moves focus to its first item. For submenu items without child items: Closes submenus, moves focus to the next main menu item, and opens its submenu. | |
| Left Arrow | For main menu items: Moves focus to the previous menu item. |
| For the first level submenu items: Closes the submenu, moves focus to the previous main menu item, and opens its submenu. For another level submenu items: Closes the current level submenu and moves focus to the corresponding parent item. |
| Shortcut Keys | Description |
|---|---|
| End | Moves focus to the last menu/submenu item. |
| Home | Moves focus to the first menu/submenu item. |
| Esc | Closes opened submenus and moves focus to the corresponding main menu item. |
| Up Arrow | Moves focus to the previous menu item. |
| Down Arrow | Moves focus to the next menu item. |
| Left Arrow | For main menu items with child items: Opens a submenu and moves focus to its last item. |
| For submenu items: Closes the submenu and moves focus to the corresponding parent item. | |
| Right Arrow | For items with child items: Opens a submenu and moves focus to its first item. |
| Shortcut Keys | Description |
|---|---|
| Down Arrow | Moves focus to the next menu item. |
| Up Arrow | Moves focus to the previous menu item. |
| Right Arrow | For items with child items: Opens a submenu. |
| Left Arrow | For main menu items with child items: Opens a submenu and moves focus to its last item. |
| For submenu items: Closes the submenu and moves focus to the corresponding parent item. |
| Shortcut Keys | Description |
|---|---|
| Down Arrow | For the hamburger button: Opens the main menu and moves focus to its first item. |
| For menu items: Moves focus to the next item without expanding/collapsing an item. | |
| Up Arrow | For the hamburger button: Opens the main menu and moves focus to its last item. |
| For menu items: Moves focus to the previous item without expanding/collapsing an item. | |
| Right Arrow | Expands the focused collapsed item or moves focus to the first child item. |
| Left Arrow | Collapses the focused expanded item or moves focus to the corresponding parent item. |
| Home | Moves focus to the first item in the tree without expanding/collapsing an item. |
| End | Moves focus to the last item in the tree without expanding/collapsing an item. |
| Esc | Closes the menu and moves focus to the hamburger button. |
If a Blazor application throws unexpected exceptions, refer to the following help topic: Troubleshooting.
Object ComponentBase DxComponentBase DevExpress.Blazor.Base.DxAsyncDisposableComponent DevExpress.Blazor.Base.DxDecoratedComponent DxComponent DxMenu
See Also