问题描述
|
在我的代码的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由子元素处理。
是否可以在InputBindings中的XAML中设置Previewkeydown?以及如何实现呢?
解决方法
您是否尝试过使用Key属性而不是Gesture?像这样:
<UserControl.InputBindings>
<KeyBinding Key=\"Up\" Command=\"{Binding ElementName=_MaschinenView,Path=UpCommand}\" />
....
</UserControl.InputBindings>