TabIndex vs. KeyboardNavigation.TabIndex in WPF
Asked Answered
S

2

20

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

Sliest answered 2/6, 2011 at 8:34 Comment(0)
G
14

@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>
Gabelle answered 17/9, 2012 at 0:34 Comment(0)
D
13

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

Delastre answered 2/6, 2011 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.