FrameworkElement.ContextMenuOpening 事件


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

 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 ... -->

(这种用法是必需的,因为 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