Back to Devexpress

DockLayoutManager.ShowingMenu Event

wpf-devexpress-dot-xpf-dot-docking-dot-docklayoutmanager-34d016f5.md

latest11.6 KB
Original Source

DockLayoutManager.ShowingMenu Event

Fires before showing a context menu, and allows it to be customized.

Namespace : DevExpress.Xpf.Docking

Assembly : DevExpress.Xpf.Docking.v25.2.dll

NuGet Package : DevExpress.Wpf.Docking

Declaration

csharp
public event ShowingMenuEventHandler ShowingMenu
vb
Public Event ShowingMenu As ShowingMenuEventHandler

Event Data

The ShowingMenu event's data class is ShowingMenuEventArgs. The following properties provide information specific to this event:

PropertyDescription
ActionListGets a collection of actions that manipulate bar objects. Inherited from ShowMenuEventArgs<T>.
HandledGets or sets a value that indicates the present state of the event handling for a routed event as it travels the route. Inherited from RoutedEventArgs.
ItemsGets a collection of BarItems that are displayed in the popup menu. Inherited from ShowMenuEventArgs<T>.
MenuGets the context menu’s type. Inherited from ShowMenuEventArgs<T>.
OriginalSourceGets the original reporting source as determined by pure hit testing, before any possible Source adjustment by a parent class. Inherited from RoutedEventArgs.
RoutedEventGets or sets the RoutedEvent associated with this RoutedEventArgs instance. Inherited from RoutedEventArgs.
ShowGets or sets whether the popup menu is displayed. Inherited from ShowMenuEventArgs<T>.
SourceGets or sets a reference to the object that raised the event. Inherited from RoutedEventArgs.
TargetElementGets the UI element for which the context menu is shown. Inherited from ShowMenuEventArgs<T>.

The event data class exposes the following methods:

MethodDescription
InvokeEventHandler(Delegate, Object)When overridden in a derived class, provides a way to invoke event handlers in a type-specific way, which can increase efficiency over the base implementation. Inherited from RoutedEventArgs.
OnSetSource(Object)When overridden in a derived class, provides a notification callback entry point whenever the value of the Source property of an instance changes. Inherited from RoutedEventArgs.

Remarks

This event allows you to respond to a context menu being invoked. You can handle it to change a menu, or prevent it from being displayed. To prevent the menu from being displayed, set the event’s Show parameter to false.

To change the menu, add bar customization actions to the event’s ActionList parameter. Bar customization actions are objects that implement the IBarManagerControllerAction interface. See DockLayoutManager.ContextMenuCustomizations for a list of most commonly used customization actions. To get information on all the available actions, see Bar Actions.

To customize context menus in XAML, use the DockLayoutManager.ContextMenuCustomizations and DockLayoutManager.ItemSelectorMenuCustomizations properties.

Example

This example shows how to customize a context menu for a LayoutPanel via the DockManager.ShowingMenu event. The menu is customized using bar customization actions. In the example, two actions are created:

  • a BarButtonItem object represents an action that adds the BarButtonItem object to the menu

  • an InsertBarItemLinkAction object represents an action that inserts a bar item link to the menu (in the example, the inserted link represents a separator).Actions are added to the event's ActionList parameter and are executed after the ShowingMenu event handler is completed.

The following image shows the menu containing the custom items:

View Example

csharp
private void dockManager1_ShowingMenu(object sender, DevExpress.Xpf.Docking.Base.ShowingMenuEventArgs e) {
    ItemContextMenu menu = (e.Menu as ItemContextMenu);
    if (menu == null) return;
    // Action 1 - Insert a new About button at the first position in the menu.
    BarButtonItem actionAddAboutItem = new BarButtonItem();
    // Specify the position for the button via the InsertBarItemLinkAction.ItemLinkIndex attached property.
    InsertBarItemLinkAction.SetItemLinkIndex(actionAddAboutItem, 0);
    actionAddAboutItem.Content = "About...";
    actionAddAboutItem.ItemClick += new ItemClickEventHandler(btnAbout_ItemClick);
    // Action 2 - Insert a separator
    InsertBarItemLinkAction actionAddSeparator = new InsertBarItemLinkAction();
    actionAddSeparator.ItemLink = new BarItemLinkSeparator();
    actionAddSeparator.ItemLinkIndex = 1;

    // Add the actions to the ActionList
    e.ActionList.Add(actionAddAboutItem);
    e.ActionList.Add(actionAddSeparator);
}

void btnAbout_ItemClick(object sender, ItemClickEventArgs e) {
    MessageBox.Show("About window");
}
vb
Private Sub dockManager1_ShowingMenu(ByVal sender As Object, ByVal e As DevExpress.Xpf.Docking.Base.ShowingMenuEventArgs)
    Dim menu As ItemContextMenu = (TryCast(e.Menu, ItemContextMenu))
    If menu Is Nothing Then
        Return
    End If
    ' Action 1 - Insert a new About button at the first position in the menu.
    Dim actionAddAboutItem As New BarButtonItem()
    ' Specify the position for the button via the InsertBarItemLinkAction.ItemLinkIndex attached property.
    InsertBarItemLinkAction.SetItemLinkIndex(actionAddAboutItem, 0)
    actionAddAboutItem.Content = "About..."
    AddHandler actionAddAboutItem.ItemClick, AddressOf btnAbout_ItemClick
    ' Action 2 - Insert a separator
    Dim actionAddSeparator As New InsertBarItemLinkAction()
    actionAddSeparator.ItemLink = New BarItemLinkSeparator()
    actionAddSeparator.ItemLinkIndex = 1

    ' Add the actions to the ActionList
    e.ActionList.Add(actionAddAboutItem)
    e.ActionList.Add(actionAddSeparator)
End Sub

Private Sub btnAbout_ItemClick(ByVal sender As Object, ByVal e As ItemClickEventArgs)
    MessageBox.Show("About window")
End Sub

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ShowingMenu event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

wpf-dock-layout-manager-use-events-to-dynamically-customize-context-menus/CS/DockManager_MenuCustomization/Window1.xaml#L12

xml
Name="dockManager1"
ShowingMenu="dockManager1_ShowingMenu">
<dxdo:LayoutGroup x:Name="rootGroup" Orientation="Horizontal">

wpf-dock-layout-manager-use-events-to-dynamically-customize-context-menus/CS/DockManager_MenuCustomization/obj/Debug/net8.0-windows/Window1.g.cs#L128

csharp
#line 12 "..\..\..\Window1.xaml"
this.dockManager1.ShowingMenu += new DevExpress.Xpf.Docking.Base.ShowingMenuEventHandler(this.dockManager1_ShowingMenu);

wpf-dock-layout-manager-use-events-to-dynamically-customize-context-menus/VB/DockManager_MenuCustomization/obj.NetFX/Debug/Window1.g.vb#L122

vb
#ExternalSource("..\..\Window1.xaml",12)
AddHandler Me.dockManager1.ShowingMenu, New DevExpress.Xpf.Docking.Base.ShowingMenuEventHandler(AddressOf Me.dockManager1_ShowingMenu)

See Also

ContextMenuCustomizations

ItemSelectorMenuCustomizations

DockLayoutManager Class

DockLayoutManager Members

DevExpress.Xpf.Docking Namespace