Event before opening the context menu

In this simple example, I want to show MessageBoxbefore the context menu is displayed.

I am writing this code in "XAML":

<Border>
    <Border.ContextMenu>
        <ContextMenu ContextMenuOpening="ContextMenu_ContextMenuOpening">
            <MenuItem Header="Select pic" IsEnabled="{Binding Path=ProductSelected}" />
            <MenuItem Header="Paste pict" Name="miPaste" Click="miPaste_Click"/>
        </ContextMenu>
    </Border.ContextMenu>
    <Image Stretch="Fill" Source="{Binding Path=Product.Picture}" />
</Border>

And I write this in 'CS':

private void ContextMenu_ContextMenuOpening(object sender, ContextMenuEventArgs e)
{
    MessageBox.Show("OPPENING");
}

I don’t know why, when I click the right mouse button, the context menu appears, but the MessageBox is never displayed.

+3
source share
1 answer

I believe that the ContextMenuOpening event should be defined for a control that has a context menu open, not a context menu. See if the code below works:

<Border ContextMenuOpening="ContextMenu_ContextMenuOpening">
    <Border.ContextMenu>
        <ContextMenu >
            <MenuItem Header="Select pic" />
            <MenuItem Header="Paste pict" Name="miPaste" />
        </ContextMenu>
    </Border.ContextMenu>
</Border>

hope this helps, believes

+9
source

All Articles