2009-09-01 126 views
107

我知道使用_而不是&,我所看到的全是Ctrl +類型快捷鍵。WPF中的鍵盤快捷鍵

按Ctrl +ž撤消,按Ctrl +小號保存等

是否有在WPF應用程序中實現這些 '標準' 呢?還是這是一個自己推出並將它們連接到任何命令/控制的情況?

回答

149

我明白標準的方法是創建命令,然後將它們的快捷鍵添加到它們作爲InputGestures。這使快捷鍵可以工作,即使它們沒有連接到任何控件。由於菜單項瞭解鍵盤手勢,因此如果將該命令掛接到菜單項,它們將自動在菜單項文本中顯示快捷鍵。

A.創建靜態屬性來保存命令(最好在靜態類屬性,你創建的命令 - 但對於簡單的例子,只是用靜態的屬性窗口中的.cs):

public static RoutedCommand MyCommand = new RoutedCommand(); 

B.添加快捷鍵(s)表示,應該調用方法:

MyCommand.InputGestures.Add(new KeyGesture(Key.S , ModifierKeys.Control)); 

C.創建命令綁定點,你的方法調用上執行,把這些在其下它應該工作的UI元素的命令綁定(如,窗口)&該方法:

<Window.CommandBindings> 
    <CommandBinding Command="{x:Static local:MyWindow.MyCommand}" Executed="MyCommandExecuted"/> 
</Window.CommandBindings> 

private void MyCommandExecuted(object sender, ExecutedRoutedEventArgs e) { ... } 
+2

如何將命令與菜單項相關聯?當然,這將是這個答案中包含的最重要的信息,但它缺失。 – Timwi 2011-02-06 13:04:31

+7

@Timwi:我用上面的代碼以這種方式爲現有事件添加鍵盤快捷鍵: RoutedCommand cmndSettings = new RoutedCommand(); cmndSettings.InputGestures.Add(new KeyGesture(Key.S,ModifierKeys.Control)); CommandBindings.Add(new CommandBinding(cmndSettings,mnuSettings_Click)); – itsho 2012-01-11 16:12:15

+1

itsho的評論爲我做了這個工作,無法使上面的xml代碼工作。 – eightx2 2012-04-03 21:56:48

9

這取決於你想在哪裏使用這些。 TextBoxBase派生的控件已經實現了這些快捷方式。如果你想使用自定義鍵盤快捷鍵,你應該看看命令和輸入手勢。這裏是從開關代碼小教程:WPF Tutorial - Command Bindings and Custom Commands

希望這會有所幫助。

+5

什麼是廢話教程 - 並沒有解釋所有的絕對最重要的事情,那就是如何使用一個命令,它不會是他們預定義的20個「通用」命令之一。 – Timwi 2011-02-06 13:08:13

+3

不幸的是,看起來那個鏈接已經死了。 – 2016-02-11 21:00:27

2

VB.Net:

Public Shared SaveCommand_AltS As New RoutedCommand 

裏面加載事件:

SaveCommand_AltS.InputGestures.Add(New KeyGesture(Key.S, ModifierKeys.Control)) 

Me.CommandBindings.Add(New CommandBinding(SaveCommand_AltS, AddressOf Me.save)) 

沒有XAML需要。

希望這會有所幫助。

-2

如何用MenuItem命令相關聯:

<MenuItem Header="My command" Command="{x:Static local:MyWindow.MyCommand}"/> 
11

試試這個代碼...

首先創建一個對象RoutedComand

RoutedCommand newCmd = new RoutedCommand(); 
    newCmd.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control)); 
    CommandBindings.Add(new CommandBinding(newCmd, btnNew_Click)); 
3

文檔化這個答案給別人,因爲有一個更簡單的可以做到這一點很少被引用的方式,並且不需要在所有接觸XAML。

要鏈接鍵盤快捷鍵,只需在Window構造函數中添加一個新的KeyBinding到InputBindings集合。作爲命令,傳入實現ICommand的任意命令類。對於執行方法,只需實現你需要的任何邏輯。在我下面的例子中,我的WindowCommand類接受一個委託,它將在調用時執行。當我構造新的WindowCommand以使用我的綁定進行傳入時,我只需在我的初始化程序中指示我希望WindowCommand執行的方法。

您可以使用此模式來創建自己的快捷鍵盤快捷鍵。

public YourWindow() //inside any WPF Window constructor 
{ 
    ... 
    //add this one statement to bind a new keyboard command shortcut 
    InputBindings.Add(new KeyBinding(//add a new key-binding, and pass in your command object instance which contains the Execute method which WPF will execute 
     new WindowCommand(this) 
     { 
     ExecuteDelegate = TogglePause //REPLACE TogglePause with your method delegate 
     }, new KeyGesture(Key.P, ModifierKeys.Control))); 
    ... 
} 

創建一個簡單的WindowCommand類,該類需要執行委託來觸發設置的任何方法。

public class WindowCommand : ICommand 
{ 
    private MainWindow _window; 

    //Set this delegate when you initialize a new object. This is the method the command will execute. You can also change this delegate type if you need to. 
    public Action ExecuteDelegate { get; set; } 

    //You don't have to add a parameter that takes a constructor. I've just added one in case I need access to the window directly. 
    public WindowCommand(MainWindow window) 
    { 
     _window = window; 
    } 

    //always called before executing the command, mine just always returns true 
    public bool CanExecute(object parameter) 
    { 
     return true; //mine always returns true, yours can use a new CanExecute delegate, or add custom logic to this method instead. 
    } 

    public event EventHandler CanExecuteChanged; //i'm not using this, but it's required by the interface 

    //the important method that executes the actual command logic 
    public void Execute(object parameter) 
    { 
     if (ExecuteDelegate != null) 
     { 
      ExecuteDelegate(); 
     } 
     else 
     { 
      throw new InvalidOperationException(); 
     } 
    } 
} 
+0

超級。它的工作完美適合我的場景...... – Geeth 2018-02-07 06:04:34

1

我有類似的問題,並發現@ aliwa的答案是最有幫助和最優雅的解決方案;不過,我需要一個特定的組合鍵,Ctrl + 1。不幸的是我得到了以下錯誤:

'1' cannot be used as a value for 'Key'. Numbers are not valid enumeration values.

有了一點進一步的搜索,我修改@ aliwa的回答如下:

<Window.InputBindings> 
    <KeyBinding Gesture="Ctrl+1" Command="{Binding MyCommand}"/> 
</Window.InputBindings> 

我發現這對相當好,我需要任意組合工作的偉大。