Keyboard access for GridViewColumnHeaders?
-
04-06-2021 - |
سؤال
A ListView requirement we have is that the column headers be accessible by keyboard, which doesn't happen with the default ListView. For example:
<StackPanel x:Name="LayoutRoot">
<ListView HorizontalAlignment="Left" KeyboardNavigation.TabNavigation="Local">
<ListView.View>
<GridView >
<GridViewColumn Header="Header _1" KeyboardNavigation.TabIndex="0"/>
<GridViewColumn Header="Header _2" KeyboardNavigation.TabIndex="1"/>
<GridViewColumn Header="header _3" KeyboardNavigation.TabIndex="2"/>
</GridView>
</ListView.View>
<ListViewItem Content="I'm the first item" />
<ListViewItem Content="I'm the second item" />
</ListView>
<Button Content="I can also get focus" HorizontalAlignment="Left" Margin="0,27,0,0"/>
</StackPanel>
The access keys show for the headers, but they don't get keyboard focus. The TabIndex and TabNavigation I added from reading this question: tabbing to TextBoxes in column headers
What do I need to do to make the GridViewColumnHeaders keyboard accessible?
المحلول
Someone internal had the solution. In the GridViewColumnHeader ControlTemplate, set the ContentPresenter to be Focusable=true, like this:
<ContentPresenter x:Name="ContentPresenter"
Style="{StaticResource ListViewContentPresenterStyle}"
ContentTemplate="{TemplateBinding ContentTemplate}"
SnapsToDevicePixels="{TemplateBinding SnapsToDevicePixels}"
RecognizesAccessKey="True"
Focusable="True"
Grid.Column="1">
Also, set the ListView KeyboardNavigation.TabNavigation=Local.
لا تنتمي إلى StackOverflow