WPF应用程序命令绑定不起作用

时间:2010-12-10 10:07:00

标签: wpf routed-commands commandbinding routedevent

您好 我在WPF中的CommandBindings有一个奇怪的问题。 我在对象的构造函数中添加CommandBindings。命令绑定看起来像

   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));

负责执行的相应函数看起来那样

 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;
    }

问题是只有cut命令有效。我的意思是如果我在任何其他功能(复制或粘贴)中设置断点,断点不会被击中。有人能告诉我我做错了吗?

2 个答案:

答案 0 :(得分:0)

CopyPaste命令是否绑定到应用程序窗口中的任何控件?看起来UI只查找Cut命令而不是其他两个命令。确保将其他两个命令绑定到UI井。

答案 1 :(得分:0)

您还需要添加KeyGestures

  InputBindings.Add(new InputBinding("YourCommand" ,ApplicationCommands.Copy.InputGestures[0])) // Default Gesture is Ctrl+C