WPF How to catch ContextMenuClosing event

时光总嘲笑我的痴心妄想 提交于 2019-12-01 20:35:11

Per the MSDN documentation...

ContextMenu itself is a FrameworkElement derived class, but the ContextMenuClosing event will not be raised by a context menu directly. Instead, the event is raised from the element that "owns" the context menu as a property and is only raised when a user attempts to close a context menu in the UI.

You would need to adjust your code so that the handler is defined solely on the DropDownButton as you have done. If there is a nested ContextMenu then the nested ContextMenu will obviously raise the event.

<local:DropDownButton ContextMenuClosing="colorPallete_ContextMenuClosing">
        ...                             
</local:DropDownButton>

Using a Button it would look like this...

<Button ContextMenuClosing="ContextMenu_ContextMenuClosing">
    <Button.ContextMenu>
        <ContextMenu>
             <MenuItem Header="Go"/>
        </ContextMenu>
    </Button.ContextMenu>
 </Button>

..where when the ContextMenu containing the MenuItem closes; the event will be raised and the handler will be called.

Not certain what DropDownButton control you are using so I can't comment on what the DropDown property is and how you are nesting your ContextMenu.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!