复制/粘贴键绑定不起作用

发布于 2024-12-27 11:36:29 字数 1080 浏览 4 评论 0原文

我的主窗口中有以下键绑定:

<KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/>
<KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/>

<KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/>
<KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/>
<KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/>

“打开”和“保存”键绑定工作正常...当我按下组合键时,其余键什么也不做。输出中没有绑定错误。我的菜单上也有绑定到相同命令的按钮,并且它们可以工作。使用与 CanExecute 方法关联的命令是否存在问题?我正在使用.Net 4.0。关于为什么剪贴板操作不起作用的任何想法?

更新: 如果我将其他东西(如 OpenCommand)绑定到 Ctrl+C,它就可以工作。如果我将 CopyCommand 绑定到不同的手势,它仍然不起作用。所以看来是命令的问题。但这很奇怪,因为我的复制按钮绑定到同一个 CopyCommand 可以正常工作。这是它绑定到的 CopyCommand 代码:

public ICommand CopyCommand
    {
        get
        {
            if (this.copyCommand == null)
            {
                this.copyCommand = new RelayCommand(
                    param => this.Copy(),
                    param => this.Copy_CanExecute());
            }

            return this.copyCommand;
        }
    }

I have the following keybindings in my MainWindow:

<KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/>
<KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/>

<KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/>
<KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/>
<KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/>

The Open and the Save keybindings work fine... the rest do nothing when I hit the key combination. There are no binding errors in the output. I also have buttons on my menu bound to the same commands and they work. Is there an issue using commands that have a CanExecute method associated with them? I an using .Net 4.0. Any ideas as to why the clipboard actions wouldn't work?

Update:
If I bind something else (like OpenCommand) to Ctrl+C it works. If I bind CopyCommand to a different gesture it still does not work. So it seems to be a problem with the command. That is strange though because my copy button works fine bound to the same CopyCommand. Here is the CopyCommand code that it is bound to:

public ICommand CopyCommand
    {
        get
        {
            if (this.copyCommand == null)
            {
                this.copyCommand = new RelayCommand(
                    param => this.Copy(),
                    param => this.Copy_CanExecute());
            }

            return this.copyCommand;
        }
    }

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

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

发布评论

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

评论(3

指尖上得阳光 2025-01-03 11:36:29

您只能执行 CanExecute 返回 true 的命令,这可能是它们不执行的原因之一。

另一个可能的原因是各个手势的本地处理,正如 TextBoxes 默认情况下所做的那样。您可以通过使用自己的命令在本地重新声明 KeyBindings 来覆盖此设置。

You can only execute commands where CanExecute returns true, might be one reason why they do no execute.

Another possible reason is local handling of the respective gestures, as TextBoxes do by default. You can override this by re-declaring the KeyBindings locally with your own command.

爱格式化 2025-01-03 11:36:29

这很好用。在我的 MainWindow.xaml 文件中,我添加了两个 keyBinding 命令以进行说明。

<Window x:Class="MainWindowCommandBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.InputBindings>
        <KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/>
        <!--<KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/>-->
        <KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/>
        <!--<KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/>
        <KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/>-->
    </Window.InputBindings>
    <Grid>

    </Grid>
</Window>

在我的 MainWindow.xaml.cs 文件中,我按如下方式初始化 DataContext。

  public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowContext();
    }

MainWindowContext 类定义如下,

class MainWindowContext
{
    RelayCommand _openCommand;
    public ICommand OpenCommand
    {
        get
        {
            if (_openCommand == null)
            {
                _openCommand = new RelayCommand(
                    param => this.Open(),
                    param => this.Open_CanExecute());
            }
            return _openCommand;
        }

        set { _openCommand = (RelayCommand) value; }

    }

    RelayCommand _copyCommand;
    public ICommand CopyCommand
    {
        get
        {
            if (_copyCommand == null)
            {
                _copyCommand = new RelayCommand(
                    param => this.Copy(),
                    param => this.Copy_CanExecute());
            }
            return _copyCommand;
        }

        set { _copyCommand = (RelayCommand)value; }

    }

    private bool Copy_CanExecute()
    {
        return true;
    }

    private object Copy()
    {
        Console.Out.WriteLine("Copy command executed");
        return null;
    }

    private bool Open_CanExecute()
    {
        return true;
    }

    private object Open()
    {
        Console.Out.WriteLine("Open command executed");
        return null;
    }
}

当我执行时,它工作正常。您可以在控制台中查看已执行的命令。
如果我错过了什么,请告诉我。

this works fine. In my MainWindow.xaml file, I add two keyBinding commands for illustration

<Window x:Class="MainWindowCommandBinding.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    Title="MainWindow" Height="350" Width="525">
    <Window.InputBindings>
        <KeyBinding Command="{Binding OpenCommand}" Gesture="Ctrl+O"/>
        <!--<KeyBinding Command="{Binding SaveCommand}" Gesture="Ctrl+S"/>-->
        <KeyBinding Command="{Binding CopyCommand}" Gesture="Ctrl+C"/>
        <!--<KeyBinding Command="{Binding PasteCommand}" Gesture="Ctrl+V"/>
        <KeyBinding Command="{Binding CutCommand}" Gesture="Ctrl+X"/>-->
    </Window.InputBindings>
    <Grid>

    </Grid>
</Window>

In my MainWindow.xaml.cs file, I initialize my DataContext as follow.

  public MainWindow()
    {
        InitializeComponent();
        DataContext = new MainWindowContext();
    }

The MainWindowContext class is defined as follow

class MainWindowContext
{
    RelayCommand _openCommand;
    public ICommand OpenCommand
    {
        get
        {
            if (_openCommand == null)
            {
                _openCommand = new RelayCommand(
                    param => this.Open(),
                    param => this.Open_CanExecute());
            }
            return _openCommand;
        }

        set { _openCommand = (RelayCommand) value; }

    }

    RelayCommand _copyCommand;
    public ICommand CopyCommand
    {
        get
        {
            if (_copyCommand == null)
            {
                _copyCommand = new RelayCommand(
                    param => this.Copy(),
                    param => this.Copy_CanExecute());
            }
            return _copyCommand;
        }

        set { _copyCommand = (RelayCommand)value; }

    }

    private bool Copy_CanExecute()
    {
        return true;
    }

    private object Copy()
    {
        Console.Out.WriteLine("Copy command executed");
        return null;
    }

    private bool Open_CanExecute()
    {
        return true;
    }

    private object Open()
    {
        Console.Out.WriteLine("Open command executed");
        return null;
    }
}

When I execute, it works fine. You can see which command has been executed in your console.
Please tell me if I miss something.

遥远的她 2025-01-03 11:36:29

有些组合键您不能使用,因为 Windows 已经使用它们。我认为 Ctrl+c/v 就是其中之一。

There are some key combinations you can not use because windows already uses them. I think Ctrl+c/v is one of them.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文