In my WPF projects I use a lot of data templates, and some of this templates require a custom tab
order. To do this I set the TabIndex
property on the relevant items inside the data template.
When I used the same template multiple times in the same page I noticed that the tab navigation didn't behave the way I wished it to (instead of jumping between indexes 1, 2, 3... in each template, and jumped between index 1 in every template, then index 2 and so forth).
After some googling I tried to set the attached property KeyboardNavigation.TabNavigation
to
"Local" (on the container in the data template). According to MSDN this way the TabIndex property
inside the container is only considered within the container.
Setting the attached property worked, but with an annoying side-effect, for example, if we have a ContentPresenter (to incorporate another data template) and a button:
<ContentPresenter Content={Binding Something} KeyboardNavigation.TabIndex="0" /> <Button KeyboardNavigation.TabIndex="1">Ok</Button>
And the container in the data template of the content presenter has the TabNavigation
property set
to "Local", this will make the button focus first, and the contents of the ContentPresenter
afterwards (I'm not sure why).
To fix this use the following code:
<ContentControl Content={Binding Something} Focusable="False" KeyboardNavigation.TabNavigation="Local" KeyboardNavigation.TabIndex="0" /> <Button KeyboardNavigation.TabIndex="1">Ok</Button>