开发者

WPF ContextMenu using (Shift-Right-Click)

开发者 https://www.devze.com 2023-02-18 16:28 出处:网络
I have a question w开发者_如何学Cith the \"ContextMenu\" in WPF. Is there a way to have the context menu pop up only if a \"Shift-Right-Click\" was performed??

I have a question w开发者_如何学Cith the "ContextMenu" in WPF. Is there a way to have the context menu pop up only if a "Shift-Right-Click" was performed?? I have been looking all over the place for this. The ContextMenu seems to only be able to pop up when a "right-click" is made.

Anyone have any idea's ??


Try this.... your XAML context menu properties should look like this...

<ElementToWhichContextMenuIsAttached ContextMenu="{StaticResource MyContextMenu}"
                                     ContextMenuOpening="MyContextMenuOpening"/>

And your code behind will look like this.

    /// <summary>
    /// This will suppress the context menu if the shift key is not pressed
    /// </summary>
    private void MyContextMenuOpening(object sender, ContextMenuEventArgs e)
    {
        // Show context menu as handled if no key is down.
        e.Handled = !(Keyboard.IsKeyDown(Key.LeftShift) || Keyboard.IsKeyDown(Key.RightShift));
    }
0

精彩评论

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