开发者

ContextMenuOpening event not trigered

开发者 https://www.devze.com 2023-04-07 23:31 出处:网络
I have a button with a context menu, but I just can get the ContextMenuOpening event to triger. <Button Name=\"a_button\"

I have a button with a context menu, but I just can get the ContextMenuOpening event to triger.

<Button Name="a_button"
        ContextMenu="{StaticResource MyContextMenu}"
        ContextMenuOpening="MyContextMen开发者_如何学运维u_Opening" >
</Button>

private void MyContextMenu_Opening(object sender, ContextMenuEventArgs e)
{
   // doesnt get here.
}

Any ideas how to make this work ?

Thanks.


ContextMenuOpening event must be handled on an ancestor of the ContextMenu not on the ContextMenu itself. If you try handling it on the ContextMenu the event only fires when you right click once ContextMenu is already open.


I've just checked your code and it works perfectly.

Here is my full xaml:

<Window x:Class="WpfApplication1.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml">
    <Window.Resources>
        <ContextMenu x:Key="MyContextMenu">
            <MenuItem Header="Send" />
        </ContextMenu>
    </Window.Resources>
    <Grid>
        <Button Name="a_button"
                ContextMenu="{StaticResource MyContextMenu}"
            ContextMenuOpening="MyContextMenu_Opening" >
        </Button>
    </Grid>
</Window>

And *.cs:

    private void MyContextMenu_Opening(object sender, ContextMenuEventArgs e)
    {

    }
0

精彩评论

暂无评论...
验证码 换一张
取 消