Manipulating tab order and focus
The Tab key in a typical user interface can be used to move the keyboard focus from one control to another (with Shift + Tab working in the reverse order), providing keyboard navigation capabilities without the need to use the mouse. This has to be logical – that is, the focus should not jump erratically all over the place, but rather move to the next logical control within the UI. The following recipe shows how to set the tab order and how to set the focus to a given control.
Getting ready
Make sure Visual Studio is up and running.
How to do it…
We'll create a personal information entry form that uses tab order in a logical manner, and places the keyboard focus where it makes sense when the form first appears:
Create a new WPF application named
CH04.TabAndFocus
.Open
MainWindow.xaml
. Add some rows and columns to theGrid
and some elements, as follows:<Grid Margin="4"> <Grid.RowDefinitions> <RowDefinition Height="Auto" /> ...