FrameworkElement.ContextMenuOpening 事件

定义

在元素上的任何上下文菜单打开时发生。Occurs when any context menu on the element is opened.

public:
 event System::Windows::Controls::ContextMenuEventHandler ^ ContextMenuOpening;
public event System.Windows.Controls.ContextMenuEventHandler ContextMenuOpening;
member this.ContextMenuOpening : System.Windows.Controls.ContextMenuEventHandler 
Public Custom Event ContextMenuOpening As ContextMenuEventHandler 

注解

若要手动打开上下文菜单,事件的处理程序应将相关事件标记为 "已处理"。To manually open context menus, handlers of the events should mark the relevant event as handled. 否则,将使用 ContextMenu 属性的现有值自动打开上下文菜单。Otherwise, the existing value of the ContextMenu property will be used to automatically open a context menu. 将事件标记为已处理会有效地取消默认操作,并且可能有机会重置 ContextMenu 属性的值,然后打开新的 ContextMenuMarking the event handled will effectively cancel the default action, and could be an opportunity to reset the value of the ContextMenu property and then open the new ContextMenu. 但是,有一个应该注意的计时问题。However, there is a timing issue you should be aware of. 为了通过 ContextMenuOpening 处理程序完全替换上下文菜单,初始上下文菜单不得为 null/空。In order to completely replace the context menu through a ContextMenuOpening handler, the initial context menu must not be null / empty. 或者,您可能需要处理事件,然后手动打开新的上下文菜单。Alternatively, you might need to handle the event and then manually open a new context menu. 有关详细信息,请参阅如何:处理 ContextMenuOpening 事件For details, see How to: Handle the ContextMenuOpening Event.

若要使用此事件作为样式中的 EventTrigger,必须引用底层附加事件:To use this event as an EventTrigger in a style, you must reference the underlying attached event:

<EventTrigger RoutedEvent="ContextMenuService.ContextMenuOpening">
  <!-- storyboard here ... -->
</EventTrigger>

(这种用法是必需的,因为公开基础服务事件 FrameworkElement 上的事件实现不会映射 ContextMenuOpening 标识符,以便在触发器中使用它)。(This usage is required because the event implementation on FrameworkElement that exposes the underlying service event does not map the ContextMenuOpening identifier such that you can use it in triggers).

ContextMenu 本身是一个 FrameworkElement 派生类,但不会从作为源打开的上下文菜单引发此事件。ContextMenu itself is a FrameworkElement derived class, but this event will not be raised from the context menu being opened as a source. 事件是从 "拥有" 上下文菜单作为属性的元素引发的,仅当用户尝试在用户界面中打开上下文菜单时才会引发该事件。The event is raised from the element that "owns" the context menu as a property and is only raised when a user attempts to open a context menu in the UI. ContextMenu 本身具有 ContextMenu 属性,但应避免此情况(有关详细信息,请参阅 FrameworkElement.ContextMenu)。It is possible for ContextMenu itself to have a ContextMenu property, but you should avoid this scenario (for details, see FrameworkElement.ContextMenu).

ContextMenu 类本身也具有类似的事件(Opened),但 Opened 不提供取消用户操作的机会。The ContextMenu class itself also has a similar event (Opened) but Opened does not provide you the opportunity to cancel the user action.

路由事件信息Routed Event Information

标识符字段Identifier field ContextMenuOpeningEvent
路由策略Routing strategy 冒泡Bubbling
委托Delegate ContextMenuEventHandler

适用于

另请参阅