Back to Devexpress

Empty Area Context Menu

windowsforms-401651-controls-and-libraries-tree-list-visual-elements-empty-area-context-menu.md

latest4.7 KB
Original Source

Empty Area Context Menu

  • Mar 01, 2024
  • 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:

csharp
void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
    if (e.HitInfo.HitInfoType == HitInfoType.Empty) {
        popupMenu_Empty.Tag = e.HitInfo;
        e.ShowCustomMenu(popupMenu_Empty);
    }
}
vb
Private Sub TreeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    If e.HitInfo.HitInfoType = HitInfoType.Empty Then
        popupMenu_Empty.Tag = e.HitInfo
        e.ShowCustomMenu(popupMenu_Empty)
    End If
End Sub

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:

csharp
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;
    }
}
vb
Imports DevExpress.Utils.Menu
Imports DevExpress.Utils.Svg
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Localization

AddHandler treeList1.PopupMenuShowing, AddressOf OnPopupMenuShowing

Private Sub OnPopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
    Dim treeList As TreeList = TryCast(sender, TreeList)
    If e.HitInfo.HitInfoType = HitInfoType.Empty Then
        Dim expandAllCaption As String = TreeListLocalizer.Active.GetLocalizedString(TreeListStringId.MenuNodeExpandAll)
        Dim expandAll As New DXMenuItem(expandAllCaption, Sub(ss, ee) treeList.ExpandAll())
        expandAll.ImageOptions.SvgImage = CommonSvgImages.Get(CommonSvgImages.Column.ExpandAll)
        e.Menu.Items.Add(expandAll)
        Dim collapseAllCaption As String = TreeListLocalizer.Active.GetLocalizedString(TreeListStringId.MenuNodeCollapseAll)
        Dim collapseAll As New DXMenuItem(collapseAllCaption, Sub(ss, ee) treeList.CollapseAll())
        collapseAll.ImageOptions.SvgImage = CommonSvgImages.Get(CommonSvgImages.Column.CollapseAll)
        e.Menu.Items.Add(collapseAll)
        e.Allow = True
    End If
End Sub

See Also

Context Menus