Getting the tab key to work in a WPF ListView control
During the summer I have been doing some work on WPF and this lead me to reading about Scott Hanselman’s new project Baby Smash. One small paragraph in this post captured my early progress
One little aside to end on. Just when I’m getting really pissed at WPF and I’m ready to give up, something simple and cool happens where I realize I’m starting to "get" it.
An example of this is a dialog that I was working on. I had a ListView control that displayed a number of rows of controls. I wanted the user to be able to tab around the dialog and through all the controls in the ListView. Sounds reasonable doesn’t it.
There are a couple of problems with the way the default behavior appears to work
- When tabbing into the ListView the container for each row of controls (a ListViewItem is treated as a tab stop
- After tabbing across the first row of controls we then tab to the control that follows the ListView, missing out the other rows in the ListView control.
It should be simple and it is simple when you know how. This is the XAML that gives us what we want.
<ListView
ItemsSource="{Binding Path=LocalObservableCollection}"
x:Name="listView1"
Focusable="True"
KeyboardNavigation.TabNavigation="Continue"
>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}">
<Setter Property="IsTabStop" Value="False" />
</Style>
</ListView.ItemContainerStyle>
<ListView.View>
<GridView>
.... grid view controls ....
</GridView>
</ListView.View>
</ListView>
The KeyboardNavigation.TabNavigation="Continue" attribute causes the tab key to tab through all the controls in the ListView, not just the first row. This corrects problem number 2.
The ListView.ItemContainerStyle is a local style to this control that stops the row container from being a tab stop, this corrects problem 1. Obviously this style will only affect this individual control, If you want to apply this style on a wider scale you may want to add it to a resource dictionary.
October 16th, 2008 at 1:14 am
Ahem, who discovered how to do this?

October 16th, 2008 at 1:41 am
[...] a bunch of child controls, you don’t get Tab key navigation between those controls for free. Derek has posted the full details on how to do this, although I’d like to point it was me who told him! There are [...]
June 24th, 2010 at 11:30 am
Thank you so much for providing this solution! You rock!!!
August 18th, 2010 at 11:18 am
#1 solution seems to work great — I’m using it on a listbox with a datatemplate for the listboxitems. It’s caused the extras “hidden” tab stop to go away.
I still have on extra tab stop leaving the listbox before it continues with the other controls. I have already set the listbox to IsTabStaop=”False”
Any ideas?