InputBinding instead of 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 this to InputBindings in XAML.

My first attempt:

 <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)); } }; 

Nothing happens to this. Most likely, a KeyDown-Event is handled by a child.

Is it possible to set preview in XAML in InputBindings? And how can this be achieved?

+4
source share
1 answer

Have you tried using the Key property instead of Gesture? Like this:

 <UserControl.InputBindings> <KeyBinding Key="Up" Command="{Binding ElementName=_MaschinenView, Path=UpCommand}" /> .... </UserControl.InputBindings> 
+1
source

All Articles