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

发布于 2024-10-07 03:41:32 字数 1543 浏览 0 评论 0原文

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

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 ??

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

煮酒 2024-10-14 03:41:33

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

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.

暗藏城府 2024-10-14 03:41:33

您还需要添加 KeyGestures

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

You need to add KeyGestures also

  InputBindings.Add(new InputBinding("YourCommand" ,ApplicationCommands.Copy.InputGestures[0])) // Default Gesture is Ctrl+C 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文