WPF How to catch ContextMenuClosing event

后端 未结 1 353
粉色の甜心
粉色の甜心 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.

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

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