Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

TabIndex vs. KeyboardNavigation.TabIndex in WPF

Tags:

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

like image 535
Strider007 Avatar asked Jun 02 '11 08:06

Strider007


2 Answers

@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>
like image 120
Simon_Weaver Avatar answered Sep 16 '22 14:09

Simon_Weaver


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

like image 45
akjoshi Avatar answered Sep 19 '22 14:09

akjoshi