Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Event before the Context Menu is opened

In this simple sample I want to show a MessageBox before the Context menu is showed.

I write this code on the ´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 the ´CS´:

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

I don't know why, when I press the right mouse button, the Context Menu appears but the MessageBox is never shown.

like image 798
Jaime Oro Avatar asked Dec 21 '22 16:12

Jaime Oro


1 Answers

I believe ContextMenuOpening event should be defined for the control which has context menu opened on, not the context menu itself. See if code below would work for you:

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

hope this helps, regards

like image 104
serge_gubenko Avatar answered Dec 24 '22 06:12

serge_gubenko