Do you want to bind Ctrl + S to the save command? This is done wrong.
You in the window in which this command should work, bind the key combination to the command.
If your command is available in the VM (that is, in the DataContext ), you do this:
<Window.InputBindings> <KeyBinding Key="S" Modifiers="Ctrl" Command="{Binding SaveCommand}" /> </Window.InputBindings>
If your team is defined somewhere else, you will need more complex code. In this case, it makes sense to attach to ApplicationCommands.Save :
<Window.InputBindings> <KeyBinding Key="S" Modifiers="Ctrl" Command="Save" /> </Window.InputBindings>
Now you need to bind your save implementation to ApplicationCommands.Save . You define two methods:
void SaveCanExecute(object sender, CanExecuteRoutedEventArgs e) { e.CanExecute = true; // ΠΈΠ»ΠΈ False, Π² Π·Π°Π²ΠΈΡΠΈΠΌΠΎΡΡΠΈ ΠΎΡ Π»ΠΎΠ³ΠΈΠΊΠΈ } void SaveExecute(object sender, ExecutedRoutedEventArgs e) { NodeSave(null, null); }
and so attached:
var saveBinding = new CommandBinding(ApplicationCommands.Save, SaveExecute, SaveCanExecute); CommandManager.RegisterClassCommandBinding(typeof(System.Windows.Window), saveBinding);
Do not forget that saving is not a matter of a window or other View-components, bring it to the VM or even to the model. (You are using MVVM, right?)
Do not forget that you can attach both a menu item and a button to the same command.