Like many users of line-of-business applications, my own users just can't understand the Tab key. For them, pressing Enter on the keyboard shouldn't submit a form, it should just move their focus to the next control.

Back in the Windows Forms days there was a neat way to achieve this functionality: You caught the KeyDown event on each of your controls and used the SelectNextControl method to move focus to the next control. In WPF, however, there's no SelectNextControl method! So how do we do something similar?

The first trick to use in WPF is to handle the PreviewKeyDown event on all your data-entry controls in one hit, rather than having to declaratively handle it individually in each control. In my case I have all my controls inside a grid, so I simply declare my grid like this:

<Grid UIElement.PreviewKeyDown="Grid_PreviewKeyDown">

What I'm doing here is telling the grid that any child control that descends from UIElement (and that includes TextBoxes and ComboBoxes) should have their PreviewKeyDown event handled by a common method called Grid_PreviewKeyDown.

So now we have caught key presses on our controls. Now, how do we tell the window to move its focus to the next available control? We don't have SelectNextControl, but we have a new method with a similar function: MoveFocus!

private void Grid_PreviewKeyDown(object sender, KeyEventArgs e)
{
var uie = e.OriginalSource as UIElement;

if (e.Key == Key.Enter)
{
e.Handled = true;
uie.MoveFocus(
new TraversalRequest(
FocusNavigationDirection.Next));
}
}

So here we're getting the control that sent us the PreviewKeyDown event, and calling its MoveFocus method to shift the focus to the next control!

So now you have no excuses! WPF can be used for line-of-business style applications after all!