2011-03-21 176 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