为其他人记录这个答案,因为有一种更简单的方法可以做到这一点,这种方法很少被引用,并且根本不需要接触 XAML。
要在 Window 级别链接键盘快捷键,只需在 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) //let's make sure the delegate was set
{
ExecuteDelegate();
}
else
{
throw new InvalidOperationException("ExecuteDelegate has not been set. There is no method to execute for this command.");
}
}
}
我确信这也可以用于其他控件,但还没有尝试过。