windowsforms-devexpress-dot-xtratreelist-dot-treelist-de49cdcd.md
Allows you to customize default context menus or invoke custom menus.
Namespace : DevExpress.XtraTreeList
Assembly : DevExpress.XtraTreeList.v25.2.dll
NuGet Packages : DevExpress.Win.Navigation, DevExpress.Win.TreeList
[DXCategory("Behavior")]
public event PopupMenuShowingEventHandler PopupMenuShowing
<DXCategory("Behavior")>
Public Event PopupMenuShowing As PopupMenuShowingEventHandler
The PopupMenuShowing event's data class is PopupMenuShowingEventArgs. The following properties provide information specific to this event:
| Property | Description |
|---|---|
| Allow | Gets or sets whether to display the context menu. Inherited from BasePopupMenuShowingEventArgs. |
| HitInfo | Provides access to information about the clicked visual element. |
| Menu | Gets or sets the control’s popup menu that will be shown. |
| MenuType | Gets the type of the context menu that is about to be shown. |
| Point | Gets or sets coordinates of the invoked context menu (top-left corner) relative to the parent control. Inherited from BasePopupMenuShowingEventArgs. |
| ScreenPoint | Gets coordinates of the invoked context menu (top-left corner) relative to the screen. Inherited from BasePopupMenuShowingEventArgs. |
The event data class exposes the following methods:
| Method | Description |
|---|---|
| ShowCustomMenu(IDXDropDownControlEx) | Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs. |
| ShowCustomMenu(ContextMenuStrip) | Invokes a custom context menu instead of the control’s menu. Inherited from BasePopupMenuShowingEventArgs. |
The Tree List shows a context menu when the user right-clicks within the following areas:
PopupMenuShowing event to populate the menu.Refer to the following help topic for more information: Context Menus.
The following code sample invokes a custom context menu when a user right-clicks a column header:
Add a BarManager to the form and create a custom PopupMenu as demonstrated in the following help topic: Popup Menus:
Handle the TreeList.PopupMenuShowing event and call the e.ShowCustomMenu method to display your custom menu instead of the default menu.
void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
if (e.MenuType == TreeListMenuType.Column) {
popupMenu_Column.Tag = e.HitInfo;
popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}";
e.ShowCustomMenu(popupMenu_Column);
}
}
TreeListHitInfo GetHitInfo(BarItemLink link) {
PopupMenu menu = link.LinkedObject as PopupMenu;
return menu.Tag as TreeListHitInfo;
}
void barButtonItem_Filter_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
TreeListHitInfo info = GetHitInfo(e.Link);
info.Column.TreeList.ShowFilterEditor(info.Column);
}
void barButtonItem_ColumnChooser_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e) {
TreeListHitInfo info = GetHitInfo(e.Link);
info.Column.TreeList.ShowCustomization();
}
Private Sub TreeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
If e.MenuType = TreeListMenuType.Column Then
popupMenu_Column.Tag = e.HitInfo
popupMenu_Column.MenuCaption = $"{e.HitInfo.Column}"
e.ShowCustomMenu(popupMenu_Column)
End If
End Sub
Private Function GetHitInfo(ByVal link As BarItemLink) As TreeListHitInfo
Dim menu As PopupMenu = TryCast(link.LinkedObject, PopupMenu)
Return TryCast(menu.Tag, TreeListHitInfo)
End Function
Private Sub barButtonItem_Filter_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
Dim info As TreeListHitInfo = GetHitInfo(e.Link)
info.Column.TreeList.ShowFilterEditor(info.Column)
End Sub
Private Sub barButtonItem_ColumnChooser_ItemClick(ByVal sender As Object, ByVal e As DevExpress.XtraBars.ItemClickEventArgs)
Dim info As TreeListHitInfo = GetHitInfo(e.Link)
info.Column.TreeList.ShowCustomization()
End Sub
The following example demonstrates how to add a custom item to a standard context menu and handle the click event for this item. The TreeList.PopupMenuShowing event is handled to add the Clear All item to the summary footer menu. Clicking this item cancels the summary calculations for all Tree List columns.
using DevExpress.Utils.Menu;
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Columns;
//...
void treeList1_PopupMenuShowing(object sender, DevExpress.XtraTreeList.PopupMenuShowingEventArgs e) {
if (e.MenuType == TreeListMenuType.Summary && e.HitInfo.HitInfoType == HitInfoType.SummaryFooter) {
DXMenuItem menuItem = new DXMenuItem("Clear All", clearAllMenuItemClick);
menuItem.Tag = e.HitInfo.Column;
e.Menu.Items.Add(menuItem);
}
}
void clearAllMenuItemClick(object sender, EventArgs e) {
TreeListColumn clickedColumn = (sender as DXMenuItem).Tag as TreeListColumn;
if (clickedColumn == null) return;
TreeList tl = clickedColumn.TreeList;
foreach (TreeListColumn column in tl.Columns)
column.SummaryFooter = SummaryItemType.None;
}
Imports DevExpress.Utils.Menu
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Columns
'...
Private Sub treeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As DevExpress.XtraTreeList.PopupMenuShowingEventArgs)
If e.MenuType = TreeListMenuType.Summary AndAlso e.HitInfo.HitInfoType = HitInfoType.SummaryFooter Then
Dim menuItem As DXMenuItem = New DXMenuItem("Clear All", AddressOf clearAllMenuItemClick)
menuItem.Tag = e.HitInfo.Column
e.Menu.Items.Add(menuItem)
End If
End Sub
Private Sub clearAllMenuItemClick(ByVal sender As Object, ByVal e As EventArgs)
Dim clickedColumn As TreeListColumn = TryCast((TryCast(sender, DXMenuItem)).Tag, TreeListColumn)
If clickedColumn Is Nothing Then Return
Dim tl As TreeList = clickedColumn.TreeList
For Each column As TreeListColumn In tl.Columns
column.SummaryFooter = SummaryItemType.None
Next
End Sub
The following sample code handles the TreeList.PopupMenuShowing event to execute the following actions:
void TreeList1_PopupMenuShowing(object sender, PopupMenuShowingEventArgs e) {
// Disable Min and Max items in the summary footer menu for the "Department" column:
if (e.MenuType == TreeListMenuType.Summary && e.HitInfo.Column.FieldName == nameof(SalesData.Department)) {
e.Menu.Find(TreeListStringId.MenuFooterMax).Enabled = false;
e.Menu.Find(TreeListStringId.MenuFooterMin).Enabled = false;
}
// Remove the "Column Chooser" item from the column header menu:
if (e.MenuType == TreeListMenuType.Column) {
e.Menu.Remove(TreeListStringId.MenuColumnColumnCustomization);
e.Menu.Remove(TreeListStringId.MenuColumnBandCustomization);
}
}
Private Sub TreeList1_PopupMenuShowing(ByVal sender As Object, ByVal e As PopupMenuShowingEventArgs)
' Disable Min and Max items in the summary footer menu for the "Department" column:
If e.MenuType = TreeListMenuType.Summary AndAlso e.HitInfo.Column.FieldName = NameOf(SalesData.Department) Then
e.Menu.Find(TreeListStringId.MenuFooterMax).Enabled = False
e.Menu.Find(TreeListStringId.MenuFooterMin).Enabled = False
End If
' Remove the "Column Chooser" item from the column header menu:
If e.MenuType = TreeListMenuType.Column Then
e.Menu.Remove(TreeListStringId.MenuColumnColumnCustomization)
e.Menu.Remove(TreeListStringId.MenuColumnBandCustomization)
End If
End Sub
See Also