WPF中的键盘快捷键

Ben*_*jol 121 wpf keyboard-shortcuts

我知道使用_而不是&,但我正在查看所有Ctrl+类型的快捷方式.

Ctrl+ Z用于撤消,Ctrl+ S用于保存等.

在WPF应用程序中实现这些是否有"标准"方法?或者是自己滚动的情况并将它们连接到任何命令/控制?

Abb*_*ner 163

我理解标准方法是创建命令,然后将快捷键作为InputGestures添加到它们.

这使得快捷键即使没有连接到任何控件也可以工作.由于菜单项了解键盘手势,如果您将该命令挂钩到菜单项,它们将自动在菜单项文本中显示您的快捷键.

  1. 创建静态属性来保存命令(最好是作为为命令创建的静态类中的属性 - 但是对于一个简单的例子,只需在window.cs中使用静态属性):

    public static RoutedCommand MyCommand = new RoutedCommand();
    
    Run Code Online (Sandbox Code Playgroud)
  2. 添加应该调用方法的快捷键:

    MyCommand.InputGestures.Add(new KeyGesture(Key.S, ModifierKeys.Control));
    
    Run Code Online (Sandbox Code Playgroud)
  3. 创建一个指向您的方法以在执行时调用的命令绑定.将它们放在UI元素的命令绑定中(例如,窗口)和方法:

    <Window.CommandBindings>
        <CommandBinding Command="{x:Static local:MyWindow.MyCommand}" Executed="MyCommandExecuted"/>
    </Window.CommandBindings>
    
    private void MyCommandExecuted(object sender, ExecutedRoutedEventArgs e) { ... }
    
    Run Code Online (Sandbox Code Playgroud)

  • @Timwi:我以这种方式使用上面的代码为现有事件添加键盘快捷键:RoutedCommand cmndSettings = new RoutedCommand(); cmndSettings.InputGestures.Add(new KeyGesture(Key.S,ModifierKeys.Control)); CommandBindings.Add(new CommandBinding(cmndSettings,mnuSettings_Click)); (7认同)
  • 如何将命令与菜单项相关联?当然,这将是包含在这个答案中的最重要信息,但它缺失了. (4认同)
  • 不幸的是,使用这种方法,命令的“已执行”代码将在(窗口或用户控件的)代码隐藏中结束,而不是在视图模型中,而不是使用通常的命令(自定义“ICommand”)执行)。 (2认同)
  • 类似示例 http://www.wpf-tutorial.com/commands/implementing-custom-commands/ (2认同)

oli*_*iwa 91

我发现这正是我在寻找与WPF中的键绑定相关的内容:

<Window.InputBindings>
        <KeyBinding Modifiers="Control"
                    Key="N"
                    Command="{Binding CreateCustomerCommand}" />
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)

请参阅博客文章MVVM CommandReference和KeyBinding

  • 对于多个修饰符,请使用 `+`,例如 `Modifiers="Control+Alt"`。请参阅 /sf/ask/283504651/ (6认同)
  • 您介意详细说明什么是“CreateCustomerCommand”以及它是如何实现的吗? (2认同)

Sha*_*nda 12

试试这个代码......

首先创建一个RoutedComand对象

  RoutedCommand newCmd = new RoutedCommand();
  newCmd.InputGestures.Add(new KeyGesture(Key.N, ModifierKeys.Control));
  CommandBindings.Add(new CommandBinding(newCmd, btnNew_Click));
Run Code Online (Sandbox Code Playgroud)


Anv*_*aka 9

这取决于你想要使用它们的位置.

TextBoxBase衍生的控件已经实现了这些快捷方式.如果要使用自定义键盘快捷键,则应查看"命令"和"输入"手势.这是Switch on the Code:WPF Tutorial - Command Bindings和Custom Commands的一个小教程

  • 什么是废话教程 - 并没有解释绝对最重要的事情,即如何使用一个不会发生在他们预定义的20个"常用"命令之一的命令. (7认同)
  • 不幸的是,这个链接已经死了. (3认同)

Ayo*_*o I 5

为其他人记录此答案,因为这样做的方法非常简单,很少被引用,并且完全不需要接触XAML。

要链接键盘快捷键,只需在Window构造函数中向InputBindings集合添加一个新的KeyBinding。作为命令,传入实现ICommand的任意命令类。对于execute方法,只需实现所需的任何逻辑即可。在下面的示例中,我的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)));
   ...
}
Run Code Online (Sandbox Code Playgroud)

创建一个简单的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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)


Nik*_*Nik 5

我遇到了类似的问题,发现@aliwa 的答案是最有帮助和最优雅的解决方案;但是,我需要一个特定的组合键Ctrl+ 1。不幸的是,我收到以下错误:

“1”不能用作“Key”的值。数字不是有效的枚举值。

通过进一步搜索,我修改了@aliwa 对以下内容的回答:

<Window.InputBindings>
    <KeyBinding Gesture="Ctrl+1" Command="{Binding MyCommand}"/>
</Window.InputBindings>
Run Code Online (Sandbox Code Playgroud)

我发现这非常适合我需要的任何组合。