TabIndex vs. KeyboardNavigation.TabIndex in WPF TabIndex vs. KeyboardNavigation.TabIndex in WPF wpf wpf

TabIndex vs. KeyboardNavigation.TabIndex in WPF


@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>


Some controls like CheckBox have TabIndex property but not all controls have this property, but you may want them to have focus and participate in focus navigation, attached property KeyboardNavigation.TabIndex can be used on in such cases. An example of such control is Hyperlink

Apart from this KeyboardNavigation class provides a lot other functionality to set focus navigation, like tab navigation behavior, KeyboardNavigationMode etc.

The navigation behavior of a navigation container can be changed by setting the attached KeyboardNavigation properties TabNavigation, ControlTabNavigation, and DirectionalNavigation. These properties are of type KeyboardNavigationMode and the possible values are Continue, Local, Contained, Cycle, Once, and None. The default value is Continue, which means the element is not a navigation container.

http://msdn.microsoft.com/en-us/library/aa969768.aspx#Keyboard_Navigation