1、创建一个类
- public class CustomCommandTest
- {
- private static RoutedUICommand customCommand;
-
- public static RoutedUICommand CustomCommand
- {
- get { return customCommand; }
- }
-
- static CustomCommandTest()
- {
- InputGestureCollection inputGestureCollection = new InputGestureCollection();
- inputGestureCollection.Add(new KeyGesture(Key.R, ModifierKeys.Control, "Ctrl+R"));
- customCommand = new RoutedUICommand("", "CustomCommand", typeof(CustomCommandTest), inputGestureCollection);
- }
- }
2、在xaml中使用该类
- <Window.CommandBindings>
- <CommandBinding Command="{x:Static local:CustomCommandTest.CustomCommand}" Executed="CustomCommand_Execute"/>
- Window.CommandBindings>
-- 其中的Executed就是摁下快捷键要调用code_behind的方法