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.