c# - 箭头键绑定不触发
问题描述
我试图让四个箭头键绑定到我的命令ViewModel
,但它们不起作用。我有一个ContentControl
这样的Window
:InputBindings
<ContentControl.InputBindings>
<KeyBinding Command="{Binding EndCmd}" Key="Esc" />
<KeyBinding Command="{Binding PanUpCmd}" Key="Up" />
<KeyBinding Command="{Binding PanDownCmd}" Key="Down" />
<KeyBinding Command="{Binding PanLeftCmd}" Key="Left" />
<KeyBinding Command="{Binding PanRightCmd}" Key="Right" />
</ContentControl.InputBindings>
在我的ViewModel
:
public RelayCommand EndCmd { get; set; }
public RelayCommand PanUpCmd { get; set; }
public RelayCommand PanDownCmd { get; set; }
public RelayCommand PanLeftCmd { get; set; }
public RelayCommand PanRightCmd { get; set; }
public MainViewModel()
{
EndCmd = new RelayCommand(End);
PanUpCmd = new RelayCommand(PanUp);
PanDownCmd = new RelayCommand(PanDown);
PanLeftCmd = new RelayCommand(PanLeft);
PanRightCmd = new RelayCommand(PanRight);
}
//functions that the commands call here
现在,Escape 键可以正常工作,但四个箭头键不能。为什么是这样?它们的设置完全相同。我想这可能与 s 有关,DataContext
所以我将KeyBinding
s 放在 Window s
InputBindings` 中,但这是同样的问题。
编辑:我已经测试了键盘上的每个键。除四个箭头键外,每个键都能正常触发。我检查Content
了是否ContentControl
吞下了这些事件,但事实并非如此。事实上,Control
它Content
有它自己的 keydown 事件,它也永远不会被调用,previewkeydown 也不会被箭头键调用。
解决方案
我复制了你的代码,它似乎工作正常。
我认为这在您的情况下不起作用的唯一原因(特别是如果Esc
有效,但不是其他键)是您在内部使用的任何内容ContentControl
也包含方向键的输入绑定。
在这种情况下,内容中的绑定将覆盖您为ContentControl
自身设置的绑定。
推荐阅读
- r - 为什么我的嵌套 ifelse 在函数外部工作,但不在内部(在 R 中)?
- ruby - 从 Slack API 获得响应的正确方法
- django - Django中的电子邮件激活在不同的浏览器中失败
- ruby-on-rails - How to include the new Stripe Checkout in Rails?
- google-apps-script - Google App Script continuefolderiteration not continuing from last folder, but restarting ever time
- ios - 删除然后将 itme 添加到 UITableView 时出现 NXException
- perl - Perl 循环仅一次
- mysql - ubuntu服务器上的mysql数据库备份(cronjob)问题
- php - Laravel (ver 6) php artisan route:list 在生产环境中返回“目标类不存在”错误(在本地环境中工作正常)
- javascript - div 布局阻止从垂直滚动到水平滚动的转换