在我的代码的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>
代码隐藏:
public ICommand UpCommand { get; set; }
UpCommand = new SimpleCommand { ExecuteDelegate = delegate { MoveFocusInDirection(Keyboard.FocusedElement, new TraversalRequest(FocusNavigationDirection.Up)); } };
这没有任何反应。很可能KeyDown-Event由Child-Element处理。
在InputBindings中设置XAML中的previewkeydown是否可行?这怎么可以实现?
答案 0 :(得分:1)
您是否尝试使用Key属性而不是Gesture?像这样:
<UserControl.InputBindings>
<KeyBinding Key="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" />
....
</UserControl.InputBindings>