Events in Blazor Menu Bar Component

26 Dec 20234 minutes to read

The Blazor Menu Bar component has a list of events that can be triggered for certain actions.

The events should be provided to the menu using MenuEvents component. When using events of menu, TValue must be provided in the MenuEvents component.

NOTE

All the events should be provided in a single MenuEvents component.

Created

The Blazor Menu Bar component’s Created event is triggered once the Menu Bar has been successfully created.

OnItemRender

The Blazor Menu Bar component’s OnItemRender is triggered while rendering each menu item.

OnOpen

The Blazor Menu Bar component’s OnOpen is triggered before opening the menu item.

OnClose

The Blazor Menu Bar component’s OnClose is triggered before closing the sub menu.

Opened

The Blazor Menu Bar component’s Opened is triggered after opening the menu item.

Closed

The Blazor Menu Bar component’s Closed is triggered after closing the menu.

ItemSelected

The Blazor Menu Bar component’s ItemSelected is triggered after selecting menu item.

@using Syncfusion.Blazor.Navigations

<SfMenu TValue="MenuItem">
    <MenuItems>
        <MenuItem Text="File">
            <MenuItems>
                <MenuItem Text="Open"></MenuItem>
                <MenuItem Text="Save"></MenuItem>
                <MenuItem Text="Exit"></MenuItem>
            </MenuItems>
        </MenuItem>
        <MenuItem Text="Edit">
            <MenuItems>
                <MenuItem Text="Cut"></MenuItem>
                <MenuItem Text="Copy"></MenuItem>
                <MenuItem Text="Paste"></MenuItem>
            </MenuItems>
        </MenuItem>
        <MenuItem Text="View">
            <MenuItems>
                <MenuItem Text="Toolbars"></MenuItem>
                <MenuItem Text="Zoomr"></MenuItem>
                <MenuItem Text="Full Screen"></MenuItem>
            </MenuItems>
        </MenuItem>
        <MenuItem Text="Tools">
            <MenuItems>
                <MenuItem Text="Spelling & Grammar"></MenuItem>
                <MenuItem Text="Customize"></MenuItem>
                <MenuItem Text="Options"></MenuItem>
            </MenuItems>
        </MenuItem>
        <MenuItem Text="Go"></MenuItem>
        <MenuItem Text="Help"></MenuItem>
    </MenuItems>
    <MenuEvents TValue="MenuItem" Created="created" OnItemRender="onItemRender" OnOpen="onOpen" OnClose="onClose" Opened="opened" Closed="closed" ItemSelected="itemSelected"></MenuEvents>
</SfMenu>
<div id="preview">@eventName Event Triggered</div>

@code {

    private string eventName = "No";

    private void created()
    {
        this.eventName = "Created";
    }

    private void onItemRender()
    {
        this.eventName = "OnItemRender";
    }

    private void onOpen()
    {
        this.eventName = "OnOpen";
    }

    private void onClose()
    {
        this.eventName = "OnClose";
    }

    private void opened()
    {
        this.eventName = "Opened";
    }

    private void closed()
    {
        this.eventName = "Closed";
    }

    private void itemSelected(MenuEventArgs<MenuItem> args)
    {
        //Selected menu item
        var selectedItem = args.Item.Text;
        this.eventName = "ItemSelected";
    }
}

<style>
    #preview {
        float: right;
        padding: 0 350px 0 0;
    }
</style>

Customizing Blazor MenuBar Items using Event