WPF Application Command Bindings doesn't work
Hi I have a strange problem with CommandBindings in WPF. I add CommandBindings in constructor of object. The command bindings looks like that
CommandBindings.Add(new CommandBinding(ApplicationCommands.Copy,Copy_Executed,Copy_Enabled));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Cut,Cut_Executed,Cut_Enabled));
CommandBindings.Add(new CommandBinding(ApplicationCommands.Paste,Paste_Executed,Paste_Enabled));
Coresponding functions which are responsible for execution look that way
private void Paste_Enabled(object sender,CanExecuteRoutedEventArgs e)
{
e.CanExecute = selectionService != null && selectionService.CurrentSelection.Count > 0;
}
private void Paste_Executed(object sender, ExecutedRoutedEventArgs e)
{
if (GetSelected() != null)
Paste(true);
else
Paste(false);
}
private void Copy_Executed(object sender, ExecutedRoutedEventArgs e)
{
Copy();
}
private void Copy_Enabled(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = selectionService.CurrentSelection.OfType<DesignerItem>().Count() > 0;
}
#endregion
private void Cut_Executed(object sender, ExecutedRoutedEventArgs e)
{
Copy();
DeleteCurrentSelection(false);
}
private void Cut_Enabled(object sender, CanExecuteRoutedEventArgs e)
{
e.CanExecute = this.SelectionService.CurrentSelection.Count() > 0;
}
The problem is that only cut command works. I mean if I set开发者_C百科 a breakpoint in any other funciotn (copy or paste) the breakpoint are not hit. Could somebody tell me what I do wrong ??
Are Copy
and Paste
commands bound to any control in your application window? Looks like the UI
only looks for Cut
command and not other two commands. Make sure you bound other two commands to the UI
well.
You need to add KeyGestures also
InputBindings.Add(new InputBinding("YourCommand" ,ApplicationCommands.Copy.InputGestures[0])) // Default Gesture is Ctrl+C
精彩评论