I have the following keybindings in my MainWindow:
<KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/>
<KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/>
<KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/>
<KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/>
<KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/>
The Open and the Save keybindings work fine… the rest do nothing when I hit the key combination. There are no binding errors in the output. I also have buttons on my menu bound to the same commands and they work. Is there an issue using commands that have a CanExecute method associated with them? I an using .Net 4.0. Any ideas as to why the clipboard actions wouldn’t work?
Update:
If I bind something else (like OpenCommand) to Ctrl+C it works. If I bind CopyCommand to a different gesture it still does not work. So it seems to be a problem with the command. That is strange though because my copy button works fine bound to the same CopyCommand. Here is the CopyCommand code that it is bound to:
public ICommand CopyCommand
{
get
{
if (this.copyCommand == null)
{
this.copyCommand = new RelayCommand(
param => this.Copy(),
param => this.Copy_CanExecute());
}
return this.copyCommand;
}
}
You can only execute commands where
CanExecutereturnstrue, might be one reason why they do no execute.Another possible reason is local handling of the respective gestures, as
TextBoxesdo by default. You can override this by re-declaring theKeyBindingslocally with your own command.