The ContextMenuOpening event occurs even if the ContextMenu is not defined in the control

Consider the following XAML:

<Window x:Class="ContextMenuEvent.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525" ContextMenuOpening="Window_ContextMenuOpening">
    <Grid>
        <Button>Ok</Button>
    </Grid>
</Window>

When you right-click the ContextMenuOpening button is launched, even if the context menu does not appear.

Why is this happening? What can I do to get at least once when the context menu REALLY opens?

I know that I can use the ContextMenu.Opened event, but this will cover only one context menu, and I want to cover all the (existing) context menus in the form.

+3
source share
2 answers

Not so sure why you will use this approach. However, you can easily check if the context menu appears. Like this:

    private void Window_ContextMenuOpening(object sender, ContextMenuEventArgs e) {
        var menu = (e.Source as FrameworkElement).ContextMenu;
        if (menu != null) {
            // etc..
        }
    }
+3

ContextMenuEventArgs System.Windows.Controls.Control( , ). ContextMenu.

0

All Articles