1

嗨,我对 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 命令有效。我的意思是,如果我在任何其他功能(复制或粘贴)中设置断点,则不会命中断点。有人可以告诉我我做错了什么吗?

4

2 回答 2

0

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

于 2010-12-10T10:20:52.143 回答
0

您还需要添加 KeyGestures

  InputBindings.Add(new InputBinding("YourCommand" ,ApplicationCommands.Copy.InputGestures[0])) // Default Gesture is Ctrl+C 
于 2010-12-10T10:36:30.780 回答