Skip to main content

Menus

  • 6 minutes to read

Many DevExpress controls have built-in context-dependent popup menus. You can customize them or replace with custom menus in code. The built-in context menus are encapsulated by DXPopupMenu class objects.

VisualElems_GridView_ColumnHeaderContextMenu

The PopupMenu component allows you to create a non-context-dependent popup menu for a control. Unlike the DXPopupMenu, the PopupMenu component provides design-time customization tools.

The current document covers information on how to customize built-in context-dependent popup menus for the DevExpress controls.

Specific DevExpress .NET controls contain properties to enable/disable predefined items in their built-in context menus. Below is a list of these controls and corresponding properties:

Example - Enable Predefined Menu Item

The following code enables the “Show Footer” menu item via the GridView.OptionsMenu.ShowFooterItem property.

gridView1.OptionsMenu.ShowFooterItem = true;

DevExpress .NET controls provide events to perform advanced menu customization tasks:

  • Customize the order, visibility and text of all predefined menu items.
  • Add custom menu items.
  • Perform custom actions when a user clicks predefined menu items.
  • Invoke custom context menus.

Control

Menu Customization Events

Data Grid

See Popup Menus.

GridView.PopupMenuShowing and GridView.GridMenuItemClick

Tree List

See Context Menus.

TreeList.PopupMenuShowing and TreeList.TreeListMenuItemClick

PivotGridControl

PivotGridControl.PopupMenuShowing and PivotGridControl.MenuItemClick

LayoutControl

See Context Menu.

LayoutControl.PopupMenuShowing

VGridControl, PropertyGridControl

VGridControlBase.PopupMenuShowing

DocumentManager

BaseView.PopupMenuShowing

DockManager

DockManager.PopupMenuShowing

SpellChecker

SpellChecker.PopupMenuShowing

SchedulerControl

SchedulerControl.PopupMenuShowing

RichEditControl

RichEditControl.PopupMenuShowing

FilterControl

FilterControl.PopupMenuShowing

PictureEdit

PopupMenuShowing,

Text Editors (TextEdit descendants)

editor.Properties.BeforeShowMenu

When you handle any of these events, you can access the currently processed menu from the e.Menu event argument. The e.Menu.Items property returns a collection of menu items, encapsulated by the following objects:

Example - Display Custom Menus

The following code sample invokes a custom context menu when a user right-clicks a column header:

Create Custom Context Menus

  1. Add a BarManager to the form and create a custom PopupMenu.

  2. Handle the GridView.PopupMenuShowing event and call the e.ShowCustomMenu method to display your custom menu instead of the default header menu.

void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.MenuType == GridMenuType.Column) {
        popupMenu_Column.Tag = e.HitInfo;
        popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}";

        e.ShowCustomMenu(popupMenu_Column);
    }
}

GridHitInfo GetHitInfo(BarItemLink link) {
    PopupMenu menu = link.LinkedObject as PopupMenu;
    return menu.Tag as GridHitInfo;
}
void barButtonItem_Filter_ItemClick(object sender, ItemClickEventArgs e) {
    GridHitInfo info = GetHitInfo(e.Link);
    info.View.ShowFilterEditor(info.Column);
}

void barButtonItem_ColumnChooser_ItemClick(object sender, ItemClickEventArgs e) {
    GridHitInfo info = GetHitInfo(e.Link);
    info.View.ShowCustomization();
}

Example - Add Custom Menu Items

The following code sample creates a menu item that fixes the column to the left. This item is added to the column header’s context menu next to the Hide This Column item:

WinForms Grid - Add Items to Context Menus

void gridView1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.MenuType == GridMenuType.Column) {
        DXMenuItem item = new DXMenuItem("Fix/Unfix This Column", (s, args) => {
            GridColumn column = (s as DXMenuItem).Tag as GridColumn;
            if (column.Fixed == FixedStyle.Left)
                column.Fixed = FixedStyle.None;
            else column.Fixed = FixedStyle.Left;
        });
        item.Tag = e.HitInfo.Column;
        int index = e.Menu.Items.IndexOf(e.Menu.Find(GridStringId.MenuColumnRemoveColumn));
        e.Menu.Items.Insert(index + 1, item);
    }
}

Display Modes

A DXPopupMenu object can be displayed as a regular menu, popup bar or RibbonMiniToolbar. To specify the menu’s display mode, set the DXPopupMenu.MenuViewType property to the required enumeration value and use a corresponding menu manager.

When you invoke a DXPopupMenu manually in code, you can specify a menu manager via the popup menu’s IDXDropDownControl.Show method. For built-in menus in DevExpress controls, you can specify a menu manager via a control’s MenuManager property.

The following menu managers are available.

  • SkinMenuManager - Displays a DXPopupMenu as a regular menu.

    DXPopupMenu-MenuViewType-Menu.png

    Note

    SkinMenuManager presents DXEditMenuItem objects as text labels without edit boxes. DXButtonGroupItem objects are displayed as sub-menus.

    // Display a DXPopupMenu as a regular menu
    UserLookAndFeel lf = UserLookAndFeel.Default;
    Control parentControl = this;
    Point pt;
    DXPopupMenu dxPopupMenu = new DXPopupMenu();
    //...
    dxPopupMenu.MenuViewType = MenuViewType.Menu;
    dxPopupMenu.ShowPopup(parentControl, pt);
    //or
    ((IDXDropDownControl)dxPopupMenu).Show(new SkinMenuManager(lf), parentControl, pt);
    
  • BarManager - Displays DXPopupMenu as a popup bar.

    DXPopupMenu-MenuViewType-Toolbar.png

    // Display a DXPopupMenu as a popup bar
    Control parentControl = this;
    Point pt;
    DXPopupMenu dxPopupMenu = new DXPopupMenu();
    //...
    dxPopupMenu.MenuViewType = MenuViewType.Toolbar;
    dxPopupMenu.ShowPopup(parentControl, pt);
    //or
    ((IDXDropDownControl)dxPopupMenu).Show(barManager1, parentControl, pt);
    
  • RibbonControl - Displays a DXPopupMenu as a RibbonMiniToolbar object.

    DXPopupMenu-MenuViewType-RibbonMiniToolbar.png

    // Display a DXPopupMenu as a RibbonMiniToolbar
    Control parentControl = this;
    Point pt;
    DXPopupMenu dxPopupMenu = new DXPopupMenu();
    //...
    dxPopupMenu.MenuViewType = MenuViewType.RibbonMiniToolbar;
    dxPopupMenu.ShowPopup(parentControl, pt);
    //or
    ((IDXDropDownControl)dxPopupMenu).Show(RibbonControl1, parentControl, pt);
    

Examples

Cheat Sheets and Best Practices

DevExpress WinForms UI controls (Data Grid, Tree List, Vertical Grid, Gantt Control, etc.) ship with the built-in context menus. Read the following quick-reference guide for detailed information and examples:

Context Menus - DevExpress WinForms Cheat Sheets

See Also