2011-03-21 175 views
4

我对WPF中的“ContextMenu”有疑问。有没有办法让上下文菜单只有在执行“Shift-Right-Click”时才弹出? 我一直在寻找这个地方。 ContextMenu似乎只有在“右键单击”时才能弹出。WPF ContextMenu使用(Shift-Right-Click)

任何人有任何想法?

回答

6

试试这个....你的XAML上下文菜单的属性应该是这样的......

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

而后面的代码如下所示。

/// <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

很好用tnx!顺便说一句,你不必使用StaticResource,你可以将其设置为内容,但“ContextMenuOpening”将与您拥有的相同。我在infragistics控件中使用它,所以我必须这样做。 – zezba9000 2011-03-21 23:00:40

+0

是的。这恰好是我最快能够掌握的例子。 – 2011-03-22 20:14:28