.cs文件中用法
private void BindCommands()
{
//[OK]
CommandBinding cb = new CommandBinding(Commands.OK, OnOK);
this.CommandBindings.Add(cb);
this.OkButton.Command = Commands.OK;
//[CANCEL]
cb = new CommandBinding(Commands.Cancel, OnCancel);
this.CommandBindings.Add(cb);
this.CancelButton.Command = Commands.Cancel;
}
然后在构造函数中调用即可。
.xmal中用法
先在.cs文件中定义: public static RoutedCommand CloseCommand = new RoutedCommand();
<Window.CommandBindings>
<CommandBinding Command="path:fileName.CloseCommand" Executed="Click_OK"/>
</Window.CommandBindings>
<Window.InputBindings>
<KeyBinding Command="path:fileName.CloseCommand" Gesture="Escape"/>
<KeyBinding Command="path:fileName.CloseCommand" Gesture="Enter"/>
</Window.InputBindings>