Tab Order
WPF Development
User Interface
XAML
Accessibility

Setting tab order in WPF

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

Tab order in WPF controls the sequence in which keyboard focus moves when the user presses Tab. A good tab order makes data entry faster, improves accessibility, and prevents the interface from feeling random or broken even when the visual layout looks correct.

Use TabIndex for Explicit Focus Order

WPF assigns a navigation order automatically based on the visual tree, but you should set TabIndex explicitly when the intended flow matters. Lower values receive focus first.

A simple form looks like this:

xml
1<Window x:Class="TabOrderDemo.MainWindow"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4        Title="Customer Form"
5        Width="360"
6        Height="220">
7    <StackPanel Margin="16">
8        <TextBlock Text="First name" Margin="0,0,0,4" />
9        <TextBox TabIndex="0" Margin="0,0,0,12" />
10
11        <TextBlock Text="Last name" Margin="0,0,0,4" />
12        <TextBox TabIndex="1" Margin="0,0,0,12" />
13
14        <TextBlock Text="Email" Margin="0,0,0,4" />
15        <TextBox TabIndex="2" Margin="0,0,0,12" />
16
17        <Button TabIndex="3" Width="100" Content="Save" />
18    </StackPanel>
19</Window>

This keeps focus movement aligned with the form's logical reading order. That matters because a visually rearranged layout does not always produce the keyboard flow you expect.

Control Which Elements Participate

Not every element should be reachable through keyboard tabbing. Labels, decorative elements, and helper text usually should not receive focus.

WPF uses IsTabStop for that decision. For controls that are focusable by default, setting IsTabStop="False" removes them from the tab sequence.

xml
1<StackPanel Margin="16">
2    <TextBlock Text="Search" Focusable="False" />
3    <TextBox TabIndex="0" Width="200" />
4    <Button TabIndex="1" Content="Run" Width="80" />
5    <Button TabIndex="2" Content="Reset" Width="80" />
6</StackPanel>

TextBlock does not normally behave like a tab stop, but the broader lesson is to be intentional. Only interactive elements that the user needs should be in the sequence.

Use Container Navigation Settings for Groups

When a window contains nested panels, tab behavior is affected by container navigation settings. WPF exposes these through KeyboardNavigation.TabNavigation.

A common example is a grouped area where you want focus to cycle within the group before moving on:

xml
1<GroupBox Header="Shipping Address"
2          KeyboardNavigation.TabNavigation="Cycle"
3          Margin="16">
4    <StackPanel Margin="8">
5        <TextBox TabIndex="0" />
6        <TextBox TabIndex="1" />
7        <TextBox TabIndex="2" />
8    </StackPanel>
9</GroupBox>

Useful values include Continue, Cycle, Contained, and Local. The right choice depends on whether the container should behave like an isolated navigation island or part of the larger form.

If you have custom dialogs or wizard steps, container-level tab navigation can make the keyboard experience much more predictable.

Set Initial Focus Deliberately

Tab order feels wrong if the first focused control is wrong. In WPF, initial focus is usually set after the window is loaded.

csharp
1using System.Windows;
2
3namespace TabOrderDemo
4{
5    public partial class MainWindow : Window
6    {
7        public MainWindow()
8        {
9            InitializeComponent();
10            Loaded += MainWindow_Loaded;
11        }
12
13        private void MainWindow_Loaded(object sender, RoutedEventArgs e)
14        {
15            FirstNameTextBox.Focus();
16        }
17    }
18}

Combined with sensible TabIndex values, this gives both the starting point and the forward navigation order a clear design.

Common Pitfalls

A frequent mistake is relying entirely on declaration order and assuming visual position will match keyboard order. As soon as a layout becomes more complex, that assumption breaks down.

Another common issue is assigning duplicate or scattered TabIndex values without thinking about the full screen flow. WPF will still navigate, but the sequence can become confusing to users.

Developers also sometimes leave disabled, hidden, or nonessential controls in the tab path. That makes keyboard use slower and hurts accessibility.

Nested containers are another source of surprises. If focus appears to skip sections or get trapped unexpectedly, inspect KeyboardNavigation.TabNavigation on the relevant parent controls.

Summary

  • Use TabIndex to define a logical, predictable keyboard flow.
  • Keep only meaningful interactive controls in the tab sequence.
  • Use IsTabStop and container navigation settings to shape focus behavior.
  • Set initial focus intentionally so tabbing starts in the right place.
  • Test tab order with the keyboard, not just by reading the XAML.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.