复制/粘贴键绑定不起作用
我的主窗口中有以下键绑定:
<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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您只能执行
CanExecute
返回true
的命令,这可能是它们不执行的原因之一。另一个可能的原因是各个手势的本地处理,正如
TextBoxes
默认情况下所做的那样。您可以通过使用自己的命令在本地重新声明KeyBindings
来覆盖此设置。You can only execute commands where
CanExecute
returnstrue
, 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 theKeyBindings
locally with your own command.这很好用。在我的 MainWindow.xaml 文件中,我添加了两个 keyBinding 命令以进行说明。
在我的 MainWindow.xaml.cs 文件中,我按如下方式初始化 DataContext。
MainWindowContext 类定义如下,
当我执行时,它工作正常。您可以在控制台中查看已执行的命令。
如果我错过了什么,请告诉我。
this works fine. In my MainWindow.xaml file, I add two keyBinding commands for illustration
In my MainWindow.xaml.cs file, I initialize my DataContext as follow.
The MainWindowContext class is defined as follow
When I execute, it works fine. You can see which command has been executed in your console.
Please tell me if I miss something.
有些组合键您不能使用,因为 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.