2009-09-01 186 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> 

我发现这对相当好,我需要任意组合工作的伟大。