WPF ContextMenu使用(Shift-Right-Click)

zez*_*000 4 wpf contextmenu right-click shift

我对WPF中的"ContextMenu"有疑问.有没有办法只在执行"Shift-Right-Click"时弹出上下文菜单?我一直在寻找这个地方.在进行"右键单击"时,ContextMenu似乎只能弹出.

有人有主意吗 ??

Cam*_*ers 6

试试这个....你的XAML上下文菜单属性应该如下所示......

<ElementToWhichContextMenuIsAttached ContextMenu="{StaticResource MyContextMenu}"
                                     ContextMenuOpening="MyContextMenuOpening"/>
Run Code Online (Sandbox Code Playgroud)

你的代码背后会是这样的.

    /// <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));
    }
Run Code Online (Sandbox Code Playgroud)