WPF How to catch ContextMenuClosing event

后端 未结 1 354
粉色の甜心
粉色の甜心 2021-01-20 14:01

I have the following XAML:


        
            
            

        
1条回答
  •  情歌与酒
    2021-01-20 14:15

    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.

    
            ...                             
    
    

    Using a Button it would look like this...

    
    

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

    0 讨论(0)
提交回复
热议问题