输入绑定而不是 PreviewKeyDown
在我的代码的 MainView 中,我有此事件来捕获 keyDown:
#region Constructor
PreviewKeyDown += SoftKeyMainPreviewKeyDown;
#endregion
private void SoftKeyMainPreviewKeyDown(object sender, KeyEventArgs e)
{
var focusedElement = Keyboard.FocusedElement;
switch (e.Key)
{
case Key.Up:
DoSomething();
break;
.....
}
}
现在我想将其移至 XAML 中的 InputBindings 中。
我的第一次尝试:
<UserControl.InputBindings>
<KeyBinding Gesture="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
....
</UserControl.InputBindings>
CodeBehind:
public ICommand UpCommand { get; set; }
UpCommand = new SimpleCommand { ExecuteDelegate = delegate { MoveFocusInDirection(Keyboard.FocusedElement, new TraversalRequest(FocusNavigationDirection.Up)); } };
没有任何反应。最有可能的是 KeyDown 事件由子元素处理。
是否可以在InputBindings的XAML中设置previewkeydown?如何实现这一目标?
In the MainView of my Code, i had this event to catch keyDown:
#region Constructor
PreviewKeyDown += SoftKeyMainPreviewKeyDown;
#endregion
private void SoftKeyMainPreviewKeyDown(object sender, KeyEventArgs e)
{
var focusedElement = Keyboard.FocusedElement;
switch (e.Key)
{
case Key.Up:
DoSomething();
break;
.....
}
}
Now I want to move that into InputBindings in the XAML.
My First try:
<UserControl.InputBindings>
<KeyBinding Gesture="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
....
</UserControl.InputBindings>
CodeBehind:
public ICommand UpCommand { get; set; }
UpCommand = new SimpleCommand { ExecuteDelegate = delegate { MoveFocusInDirection(Keyboard.FocusedElement, new TraversalRequest(FocusNavigationDirection.Up)); } };
With this nothing happens. Most probably the KeyDown-Event is handled by an Child-Element.
Is it posible to set previewkeydown in XAML in InputBindings? And how can this be achieved?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用 Key 属性而不是 Gesture ?像这样:
Have you tried using the Key property instead of Gesture? Like this: