1

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 a breakpoint in any other funciotn (copy or paste) the breakpoint are not hit. Could somebody tell me what I do wrong ??

losieko
  • 271
  • 3
  • 5

2 Answers2

0

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.

decyclone
  • 30,394
  • 6
  • 63
  • 80
0

You need to add KeyGestures also

  InputBindings.Add(new InputBinding("YourCommand" ,ApplicationCommands.Copy.InputGestures[0])) // Default Gesture is Ctrl+C 
TalentTuner
  • 17,262
  • 5
  • 38
  • 63