Создай класс команд, для примера
Код | public class MyCommands { private static RoutedUICommand calcCommand;
static MyCommands() { InputGestureCollection inputC = new InputGestureCollection(); inputC.Add(new KeyGesture(Key.C, ModifierKeys.Alt, "Alt+C")); calcCommand = new RoutedUICommand("Калькулатор", "CalcCommand", typeof(MyCommands), inputC); }
public static RoutedUICommand CalcCommand { get { return calcCommand; } } }
|
а в коде потом
Код | private void CommandBindingInitialize() { CommandBinding calcBinding = new CommandBinding(MyCommands.CalcCommand); calcBinding.Executed += new ExecutedRoutedEventHandler(calcBinding_Executed); this.CommandBindings.Add(calcBinding); } void calcBinding_Executed(object sender, ExecutedRoutedEventArgs e) { try { string system32 = Environment.GetFolderPath(Environment.SpecialFolder.System); Process.Start(system32 + "\\calc.exe"); } catch (Exception ex) { string err = ex.Message; string source = ex.Source; } }
|
И всё, можно и иногда нужно обработать CanExecute, или понастроить чего то ещё . Привязать в XAML кде надо. |