angela giesel

angela giesel

  • NA
  • 1
  • 2.4k

WPF Keyboard-Shortcuts

May 31 2012 5:02 AM
Hi,

In my WPF-application (.NET4) I need to be able to operate certain Textboxes by either manual input or by using keyboard-shortcuts. In order to make the shortcuts available across my whole application I defined them inside my main-window:

// mainwindow.xaml
<Window ...>
<Window.CommandBindings>
<CommandBinding Command=
"{x:Static local:MainWindow.upCmd}" Executed="UpCmd" />
</Window.CommandBindings>
</Window>

// mainwindow.xaml.cs
public static RoutedCommand upCmd = new RoutedCommand();
// mainwindow constructor
upCmd.InputGestures.Add(new KeyGesture(Key.Up));
// method
private void UpCmd(object sender, ExecutedRoutedEventArgs e)
{
...
}


My problem is, that as soon as I click into another Textbox all my keyboard-shortcuts won't work anymore, I assume because the main window loses focus?
So how can I enable my keyboard shortcuts to execute some special operations and at the same time beeing able to allow my Textbox to be set by typing in values via the keyboard?

Thanks!