Setting tab order in WPF
Asked Answered
S

6

95

How do I set tab ordering in WPF? I have an ItemsControl with some items expanded and some collapsed and would like to skip the collapsed ones when I'm tabbing.

Any ideas?

Sexcentenary answered 11/12, 2008 at 15:40 Comment(4)
Jesse's proposed answer is the way to go... – Pooley
that appears to be the default behaviour? – Celeski
@Sexcentenary who is Jesse? – Resht
@Stealth Rabbi: the selected answer. The name might have changed (from 11 years ago πŸ˜†) – Pooley
R
67

You can skip elements in the tab sequence by setting KeyboardNavigation.IsTabStop on the element in XAML.

KeyboardNavigation.IsTabStop="False"

You can setup a trigger that would toggle this property based on the expanded state.

Rustcolored answered 11/12, 2008 at 19:34 Comment(3)
You can also is Focusable="False" which removes it from Keyboard focus as well as tab order. – Auteur
@Geoff I did this with a bunch of buttons and it didn't work, just fyi. – Iseult
This allows for skipping controls that you don't want to be part of the tab ordering, but there are a lot of other problems in WPF. Tab orderings are not hierarchical so, if there are other controls on screen that have a zero ordering (which they do by default) then tab ordering just won't work. – Stability
M
95

If you want to explicitly set the tab ordering for elements in your form, the following attached property is supposed to help:

<Control KeyboardNavigation.TabIndex="0" ... />

I say "supposed to help" as I haven't found it very reliable though I probably need to read more about how it is intended to be used. I only post this half baked answer because no one else mentioned this property.


Note that in Win RT, the property is just TabIndex="0".

Malaria answered 26/2, 2009 at 17:52 Comment(3)
The trick is to use TabIndex with various values of KeyboardNavigation.TabNavigation in the parent element. I find myself using "Local" TabNavigation most often. :) – Bahaism
I've had to add this to each of my controls, instead of just adding them to the different StackPanels that hosts said controls. – Close
I find setting <DockPanel Panel.ZIndex="2"> helps to order entry point of top level panels. – Pageantry
R
67

You can skip elements in the tab sequence by setting KeyboardNavigation.IsTabStop on the element in XAML.

KeyboardNavigation.IsTabStop="False"

You can setup a trigger that would toggle this property based on the expanded state.

Rustcolored answered 11/12, 2008 at 19:34 Comment(3)
You can also is Focusable="False" which removes it from Keyboard focus as well as tab order. – Auteur
@Geoff I did this with a bunch of buttons and it didn't work, just fyi. – Iseult
This allows for skipping controls that you don't want to be part of the tab ordering, but there are a lot of other problems in WPF. Tab orderings are not hierarchical so, if there are other controls on screen that have a zero ordering (which they do by default) then tab ordering just won't work. – Stability
S
26

<Control KeyboardNavigation.TabIndex="0" ... /> Works perfectly fine... For example-

<ComboBox Height="23" 
          Margin="148,24,78,0" 
          Name="comboBoxDataSet"
          VerticalAlignment="Top"
          SelectionChanged="comboBoxDestMarketDataSet_SelectionChanged"
          DropDownOpened="comboBoxDestMarketDataSet_DropDownOpened"
          KeyboardNavigation.TabIndex="0" />
<ComboBox Height="23" 
          Margin="148,56,78,0" 
          Name="comboBoxCategory" 
          VerticalAlignment="Top" 
          SelectionChanged="comboBoxDestCategory_SelectionChanged"
          DropDownOpened="comboBoxDestCategory_DropDownOpened"
          KeyboardNavigation.TabIndex="1" />

Will allow you to navigate through these two combo boxes using TAB key.

Serpentine answered 25/5, 2010 at 3:16 Comment(0)
E
13

I think there is a much easier solution here, at the top within your control or window, you could add:

KeyboardNavigation.TabNavigation="Cycle"

This also automaticaly ignores the collapsed tabs.

Ephemeron answered 30/11, 2011 at 13:50 Comment(0)
B
6

Another alternative that has worked for me in the past is to simply remove all explicit TabIndex statements, and let the controls use the order that they're declared in XAML work their magic.

This, of course, may require you to reorder your controls. But this is a simple copy-paste operation.

Bodkin answered 6/6, 2014 at 16:59 Comment(2)
The problem is you can't simply reorder controls in things like DockPanels where the order you add them doesn't match the tab order. For instance, consider docking buttons on the right. You dock the last one first, the second to last second, etc. but you want the tab order to be reversed. That's what trips me up the most. Times like this I miss the old VB6 days (not the language, just this feature) where you choose 'Set tab order' and just click through all your controls. Simple and sweet. Frustrating we don't have anything like that here in 2018. – Perce
@Mark or 2020 :). Tab ordering in WPF is insanely complicated, whenever I need custom ordering I must spend the entire day wrestling with its unpredictability. In Winforms it was a mundane task that takes few minutes per window. – Reciprocal
F
1

You can use KeyboardNavigation.TabNavigation="None" to completely skip the Tabbing for specific control.

Fluctuate answered 22/6, 2020 at 18:38 Comment(0)

© 2022 - 2024 β€” McMap. All rights reserved.