Skip to main content

Custom Menus

  • 3 minutes to read

The Tree List control supports context menus for column headers, group summary footers and the total summary footer. However, it’s possible to implement custom menus for any other Tree List element.

Custom Menus

You can create a custom context menu and then display it when an end-user right-clicks the required Tree List element. To show the custom menu, handle the control’s MouseDown event. Since this event fires when any Tree List element is clicked, you need to identify the Tree List element that has been clicked. To do this, call the TreeList.CalcHitInfo method within the MouseDown event handler.

The TreeList.CalcHitInfo method takes a point, relative to the Tree List’s top-left corner, as a parameter. The method’s result is a TreeListHitInfo object identifying the Tree List element located at the specified point.

Example

The example below shows how to display a custom context menu with a click on a node indicator. The menu contains the Delete Node command.

image

The TreeList.PopupMenuShowing event fires when the default menu is about to be shown. The TreeList.CalcHitInfo method in the event handler returns internal information about the clicked visual element. If a node indicator is clicked, the Delete Node command is added to the menu. The DXMenuItem.Click event handler removes the node.

using DevExpress.Utils.Menu;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;

private void treeList1_PopupMenuShowing(object sender, DevExpress.XtraTreeList.PopupMenuShowingEventArgs e) {
    // Check if a node's indicator cell is clicked.
    TreeListHitInfo hitInfo = (sender as TreeList).CalcHitInfo(e.Point);
    TreeListNode node = null;
    if (hitInfo.HitInfoType == HitInfoType.RowIndicator) {
        node = hitInfo.Node;
    }
    if (node == null) return;
    // Create the Delete Node command.
    DXMenuItem menuItem = new DXMenuItem("Delete Node", this.deleteNodeMenuItemClick);
    menuItem.Tag = node;
    e.Menu.Items.Add(menuItem);
}

private void deleteNodeMenuItemClick(object sender, EventArgs e) {
    DXMenuItem item = sender as DXMenuItem;
    if (item == null) return;
    TreeListNode node = item.Tag as TreeListNode;
    if (node == null) return;
    node.TreeList.DeleteNode(node);
}