Setting Tabindex should work in WPF as well as WinForms. You can set it in the XAML. E.g
<Button x:Name="Button1" TabIndex="2" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="0"/>
<Button x:Name="Button2" TabIndex="1" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="1"/>
<Button x:Name="Button3" TabIndex="0" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="2"/>
Or in the Properties window of each control. Or in the Code behind e.g.
Button1.TabIndex = 2;
Button2.TabIndex = 0;
Button3.TabIndex = 1;
Under normal circumstances WPF will assign tab order based on the logical tree (
Tab Order | 2,000 Things You Should Know About WPF[
^]). If it is not behaving as expected have you catered for container controls properly?
Probably the best way would be to not use TabIndex at all but cut&paste your code so that the controls are ordered in the XAML in the order you wish them to have tabindex e.g.
<Button x:Name="Button2" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="1"/>
<Button x:Name="Button1" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="0"/>
<Button x:Name="Button3" Height="60" Width="75" Margin="221,26,221,20" Grid.Row="2"/>
Note when removing a TabIndex it may be worth checking that the Property (in the Properties window) has reverted to the default value of 2147483647.
If it is still not working as you wish, then simplify your form as much as possible to still demonstrate the problem and post the XAML by using the
Improve question link
[EDIT - As requested some "full" solutions]
This (entire) code demonstrates that it is the order that the controls appear in the XAML that determines tab order when tab index itself is not changed (i.e. the Properties window has tab index default = 2147483647)
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
<Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>
<TextBox x:Name="TextBox1" Grid.Column="0" Grid.Row="0" Text="TextBox1" />
<TextBox x:Name="TextBox2" Grid.Column="2" Grid.Row="1" Text="TextBox2" />
<TextBox x:Name="TextBox3" Grid.Column="1" Grid.Row="1" Text="TextBox3" />
<TextBox x:Name="TextBox4" Grid.Column="1" Grid.Row="0" Text="TextBox4" />
<TextBox x:Name="TextBox5" Grid.Column="2" Grid.Row="2" Text="TextBox5" />
<TextBox x:Name="TextBox6" Grid.Column="0" Grid.Row="2" Text="TextBox6" />
</Grid>
</Window>
The text in the textboxes shows the order that I am expecting the controls to get focus when I use the tab key. The form itself looks a little like this when first loaded:
TextBox1 TextBox4 (BlankCell)
(BlankCell) TextBox3 TextBox2
TextBox6 (BlankCell) TextBox5
This version of the code has the textboxes being tabbed into in reverse order i.e. TextBox6 is the first in the order, then 5, 4, 3, 2, 1
<Window x:Class="MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid>
<Grid.RowDefinitions><RowDefinition/><RowDefinition/><RowDefinition/></Grid.RowDefinitions>
<Grid.ColumnDefinitions><ColumnDefinition/><ColumnDefinition/><ColumnDefinition/></Grid.ColumnDefinitions>
<TextBox x:Name="TextBox1" Grid.Column="0" Grid.Row="0" Text="TextBox1" TabIndex ="5"/>
<TextBox x:Name="TextBox2" Grid.Column="2" Grid.Row="1" Text="TextBox2" TabIndex ="4"/>
<TextBox x:Name="TextBox3" Grid.Column="1" Grid.Row="1" Text="TextBox3" TabIndex ="3"/>
<TextBox x:Name="TextBox4" Grid.Column="1" Grid.Row="0" Text="TextBox4" TabIndex ="2"/>
<TextBox x:Name="TextBox5" Grid.Column="2" Grid.Row="2" Text="TextBox5" TabIndex ="1"/>
<TextBox x:Name="TextBox6" Grid.Column="0" Grid.Row="2" Text="TextBox6" TabIndex ="0"/>
</Grid>
</Window>
Behaviour of the form is the same if I substitute the XAML TabIndex setting for code behind when loading the form. Note there is no other code in the project (other than the automatically generated Application.xaml etc) and I targeted .NET Framework 3.5.
If your issue is that the first TextBox is not being selected until after hitting the Tab key for the first time then try this:
<Grid FocusManager.FocusedElement="{Binding ElementName=TextBox1}">
Notice the change in behaviour - with this FocusManager TextBox1 has focus once the form has loaded and the Tab key moves us to TextBox6. Without the FocusManager nothing has focus when the form is loaded and the Tab key moves us straight to TextBox6