#640 – Set the TabIndex Property to Change Tab Order

By default, the sequence of controls that get keyboard focus when the user clicks on the Tab key is based on their order in the logical tree.

If you want to change this Tab order, however, you can set the TabIndex property of each control.  The TabIndex value should start a 0 for the first control that should receive focus and then proceed sequentially, i.e. 1, 2, 3, etc.  Controls with a lower TabIndex will therefore get focus before controls with a higher TabIndex.

    <StackPanel Orientation="Vertical">
        <Button Content="Harpo" TabIndex="1" HorizontalAlignment="Center" Margin="5"/>
        <Button Content="Chico" TabIndex="0" HorizontalAlignment="Center" Margin="5"/>
        <Button Content="Groucho" TabIndex="2" HorizontalAlignment="Center" Margin="5"/>
    </StackPanel>

Advertisement

#639 – Default Tab Order Is Sensible

In WPF, you can use the Tab key to cycle through all of the focusable controls in a window, changing which control currently has focus.

You normally don’t need to worry about the tab order for controls in a window or page because WPF sets the tab order to match the logical tree.  If you start an application and press the Tab key, the first control in the logical tree will receive focus.

If you then continue to press Tab, each control in the logical tree that has its IsTabStop property set will receive focus.  The order will match the logical tree, starting at the top-level parent and working in a depth-first fashion through all child controls.

In the example below, we have several nested StackPanelin a window.  The numbers indicate the effective tab order.