Click here to Skip to main content
15,507,526 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi I am working on a WPF based application in C#. The application contains a Windows based Form containing many controls. I need to implement Tab order on the Windows based Form.
How can this be obtained. Remember that Application is WPF based while Form is Windows based Form.
Thanks

What I have tried:

I have followed rules by setting Tab Stop property and defining Tab index property for all controls. But it doesn't work for WPF based application.
However it works on Windows Form based application.
Posted
Updated 17-Apr-17 5:46am

1 solution

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.
C#
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)
HTML
<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
HTML
<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:
HTML
<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
 
Share this answer
 
v2
Comments
Wahaj Khan 18-Apr-17 7:10am    
Sorry. This no more working...To be more specific follow these Steps:
1. Create a WPF application using C#.
2. Add a New Windows based form .
3. Place some textboxes on the screen. (say 5 or 6)
4. Set Tab Index to all controls.
5. Display the Form by executing the application.
6. Kindly send the application after testing the tab order.
Thanks
CHill60 18-Apr-17 7:13am    
Tell you what - why don't you post a very simple set of sample XAML that doesn't work as I suggested. The snippets of code in my solution were all taken from such a simple example, and yes, I did test the tab order before posting.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900