TabIndex vs. KeyboardNavigation.TabIndex in WPF

Strider007 picture Strider007 · Jun 2, 2011 · Viewed 12.3k times · Source

What is the difference between TabIndex and KeyboardNavigation.TabIndex in WPF? When to use each?

Answer

Simon_Weaver picture Simon_Weaver · Sep 17, 2012

@akjoshi included a very important piece of information about TabIndex in his answer but I thought a little more explanation would help.

If you have an ItemsControl repeating an item you will end up with a tab order like this if you're not careful.

enter image description here

The solution is simple :

Apply this attached property to the main container of each repeated item.

KeyboardNavigation.TabNavigation="Local" 

This enumeration has all kinds of values, but this is the one to use for nested controls.

Note I've set IsTabStop=false for the ItemsControl itself (and no this isn't the actualy code for the graphic above).

<ItemsControl ItemsSource="{Binding CurrentItem.CustomsItems}" IsTabStop="False">
    <ItemsControl.ItemTemplate>
        <DataTemplate>
            <ctl:CustomsItem KeyboardNavigation.TabNavigation="Local" Margin="0,0,0,8"/>
        </DataTemplate>
    </ItemsControl.ItemTemplate>
</ItemsControl>