Skip to main content

DevExpress v24.2 Update — Your Feedback Matters

Our What's New in v24.2 webpage includes product-specific surveys. Your response to our survey questions will help us measure product satisfaction for features released in this major update and help us refine our plans for our next major release.

Take the survey Not interested

Empty Area Context Menu

  • 2 minutes to read

The control can show a context menu when a user right-clicks the area below nodes. The default context menu for this area does not contain items.

The Tree List raises the TreeList.PopupMenuShowing event before the control shows a context menu. Handle this event to invoke a custom menu or populate the default menu with items. Use the e.HitInfo.HitInfoType property to obtain the clicked visual element. If this property is set to Empty, a user right-clicks an empty area.

#Show Custom Menu

The following code sample invokes a custom context menu (PopupMenu) when a user right-clicks an empty area:

WinForms TreeList - Custom Empty Area Menu

void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.HitInfo.HitInfoType == HitInfoType.Empty) {
        popupMenu_Empty.Tag = e.HitInfo;
        e.ShowCustomMenu(popupMenu_Empty);
    }
}

Refer to the following help topic for more information: Custom Context Menus.

#Populate the Default Menu

The PopupMenuShowing event is raised before a context menu is shown and allows you to customize it. The HitInfo event argument is set to Empty when a user right-clicks an empty area.

The code below adds the Full Collapse and Full Expand buttons to the empty area context menu:

image

using DevExpress.Utils.Menu;
using DevExpress.Utils.Svg;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Localization;

treeList1.PopupMenuShowing += OnPopupMenuShowing;

void OnPopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    TreeList treeList = sender as TreeList;
    if(e.HitInfo.HitInfoType == HitInfoType.Empty) {
        string expandAllCaption = TreeListLocalizer.Active.GetLocalizedString(TreeListStringId.MenuNodeExpandAll);
        DXMenuItem expandAll = new DXMenuItem(expandAllCaption, (ss, ee) => treeList.ExpandAll());
        expandAll.ImageOptions.SvgImage = CommonSvgImages.Get(CommonSvgImages.Column.ExpandAll);
        e.Menu.Items.Add(expandAll);
        string collapseAllCaption = TreeListLocalizer.Active.GetLocalizedString(TreeListStringId.MenuNodeCollapseAll);
        DXMenuItem collapseAll = new DXMenuItem(collapseAllCaption, (ss, ee) => treeList.CollapseAll());
        collapseAll.ImageOptions.SvgImage = CommonSvgImages.Get(CommonSvgImages.Column.CollapseAll);
        e.Menu.Items.Add(collapseAll);
        e.Allow = true;
    }
}
See Also