DxMenu Class

A Menu component.

Namespace: DevExpress.Blazor

Assembly: DevExpress.Blazor.v21.2.dll

Declaration

public class DxMenu :
    DxControlComponent<DxMenu, MenuJSInteropProxy>,
    IMenuJSInteropProxyServer,
    IJSCallback,
    IModelWrapper<IMenuModel>

Remarks

<DxMenu> adds a menu to your Blazor application.

Menu Overview

Run Demo Watch Video

Add Menu to a Project

To add a <DxMenu> component to an application, follow the steps below:

  1. Create an application. If you use Microsoft project templates, configure the application as described in this topic: Microsoft Templates (NuGet Packages).
  2. Add the <DxMenu></DxMenu> markup to your application.
  3. Configure the component: add items, change the orientation, specify display mode, and so on (see the sections below).

Items

You can specify menu items between the <DxMenu> and </DxMenu> tags or add them to 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, CssClass properties. To add a separator before an item, use the BeginGroup property.

<div class="card">
    <DxMenu Title="DevExpress"
            ItemsPosition="ItemPosition.Start">
        <Items>
            <DxMenuItem Text="Products" IconCssClass="oi oi-layers">
                <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>

Menu

Run Demo: Menu - Overview

Orientation

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="Products" IconCssClass="oi oi-layers">
                <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>
        </Items>
    </DxMenu>
</div>

Vertical Menu

Run Demo: Menu - Orientation

Display Modes

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 code below demonstrates how to specify display mode:

<div>
    <DxMenu Title="DevExpress"
            Orientation="Orientation.Vertical"
            DisplayMode="MenuDisplayMode.Desktop">
        <Items>
            <DxMenuItem Text="Products" IconCssClass="oi oi-layers">
                <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>
        </Items>
    </DxMenu>
</div>

Desktop Vertical Menu

Desktop Vertical Menu

Run Demo

Desktop Horizontal Menu

Desktop Horizontal Menu

Run Demo

Mobile Horizontal Menu

Mobile Horizontal Menu

Run Demo

Mobile Vertical Menu

Mobile Vertical Menu

Data Binding

The Menu component supports binding to data. In the bound mode, items from the data source automatically populate menu items.

Follow the steps below to bind Menu to data:

  1. Use the Data property to specify a data source. You can use different data types:

    • Flat data - a collection of items that are available at one level.
    • Hierarchical data - a collection of nested items.
  2. Add the DataMappings tag to the component’s markup.

  3. Create the DxMenuDataMapping instance and map item properties (BeginGroup, CssClass, and so on) to data source fields. Mappings are used to link the Menu data model to the data source.

    • Flat data - the Key and ParentKey properties are required. If the Menu’s structure consists of only one level, you can omit these properties.

    • Hierarchical data - the Children property is required.

    You can also create multiple DxMenuDataMapping instances to specify different mappings for different item levels. Use the Level property to specify the item level for which data mappings are applied.

The code below 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 w-100">
    <div class="card-header p-0 font-weight-normal">
        <DxMenu ItemClick="@(e => ((TextFormattingMenuItem)e.ItemInfo.Data).Click())"
                Data="@MenuItems">
            <DataMappings>
                <DxMenuDataMapping Text="Text"
                                   Children="Children"
                                   BeginGroup="BeginGroup"
                                   CssClass="CssClass"/>
            </DataMappings>
        </DxMenu>
    </div>
    <div class="card-body">
        <div style="@Formatting.GetStyleString()">
            <span class="dxbs-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);

Data Binding

Run Demo: Menu - Data Binding

Adaptivity

The Menu component supports adaptive mode. The following properties specify how the menu responds when the width of the browser window changes and there is not enough space to display all the items:

<div class="card w-auto">
    <DxMenu Title="DevExpress"
            CollapseItemToIconMode="MenuCollapseItemToIconMode.Sequentially"
            CollapseItemsToHamburgerMenu="true">
        <Items>
            <DxMenuItem Text="Products" IconCssClass="oi oi-layers" />
            <DxMenuItem Text="Support" IconCssClass="oi oi-person" />
            <DxMenuItem Text="Documentation" IconCssClass="oi oi-book" />
            <DxMenuItem Text="Demos" IconCssClass="oi oi-monitor" />
            <DxMenuItem Text="Blogs" IconCssClass="oi oi-bell" />
            <DxMenuItem Text="Search" IconCssClass="oi oi-magnifying-glass" />
        </Items>
    </DxMenu>
</div>

Adaptivity

Run Demo: Menu - Adaptivity Watch Video

Templates

The Menu component allows you to use templates to customize the layout and appearance of the menu title and menu items.

Title Template

Use the TitleTemplate property to specify the template for the menu title.

<div class="card w-50">
    <DxMenu Title="DevExpress Logo">
        <TitleTemplate>
            <img width="120" src="images/Logo-Monochrome.svg" alt="@context" />
        </TitleTemplate>
        <Items>
            <DxMenuItem Text="Products" IconCssClass="oi oi-layers" />
            <DxMenuItem Text="Support" IconCssClass="oi oi-person" />
            <DxMenuItem Text="Documentation" IconCssClass="oi oi-book" />
            <DxMenuItem Text="Demos" IconCssClass="oi oi-monitor" />
        </Items>
    </DxMenu>
</div>

Title Template

Item Templates

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 demonstrates how to use templates to create the Search and User Profile menu items.

<div class="card">
    <DxMenu>
        <Items>
            <DxMenuItem Text="Products" IconCssClass="oi oi-layers">
                <Items>
                    <DxMenuItem Text="Subscriptions / Packs" />
                </Items>
            </DxMenuItem>
            <DxMenuItem Text="Support" IconCssClass="oi oi-people">
                <Items>
                    <DxMenuItem Text="Knowledge Base" />
                </Items>
            </DxMenuItem>
            <DxMenuItem CssClass="ml-auto">
                <Template>
                    <div class="container">
                        <input type="text" class="form-control form-control-sm center" placeholder="Search...">
                    </div>
                </Template>
            </DxMenuItem>
            <DxMenuItem Text="User Profile">
                <TextTemplate>
                    <div class="oi oi-person" />
                </TextTemplate>
                <SubMenuTemplate>
                    <div class="w-100">
                        <div class="flex-column align-items-center justify-content-center">
                            <div class="logo-container d-flex align-items-center justify-content-center mt-2">
                                <div class="oi oi-person text-muted h1" />
                            </div>
                            <div class="user-name-container bm-3 mb-2">
                                <div class="user-name-title text-muted text-center">User Name</div>
                                <div class="user-name text-center">John Heart</div>
                            </div>
                            <div class="d-flex justify-content-center mb-2">
                                <DxButton Text="Log Off" RenderStyle="@ButtonRenderStyle.Secondary"></DxButton>
                            </div>
                        </div>
                    </div>
                </SubMenuTemplate>
            </DxMenuItem>
        </Items>
    </DxMenu>
</div>

Templates

Run Demo: Menu - Templates

Inheritance

Object
ComponentBase
DevExpress.Blazor.Base.DxAsyncDisposableComponent
DevExpress.Blazor.Base.DxDecoratedComponent
DxComponentBase
DxComponentBase<DevExpress.Blazor.Internal.JSInterop.MenuJSInteropProxy>
DxControlComponent<DxMenu, DevExpress.Blazor.Internal.JSInterop.MenuJSInteropProxy>
DxMenu
See Also