我的树视图中的键盘导航有问题。 我正在实现一个3列表。第一列中的每个元素由3列组成: 一个Icon,一个textBlock和一个扩展器。如果元素的属性“hasItems”为false,则隐藏扩展器。我的问题是我无法使用向上和向下箭头在树上上下导航。我试图将扩展器“IsTabStop”属性设置为true,但这不会给我所需的结果。 感觉就像扩展器成为焦点一样:向上和向下箭头只能在扩展器上进行聚焦(当我移除扩展器时导航按预期运行)。
这是某种伪代码,只是为了解释我的雇佣关系:
<ScrollViewer >
<TreeView ItemsSource="{Binding TreeRootInstance}">
<TreeView.Resources>
<DataTemplate x:Key="CellTemplate">
<Border >
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition />
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Image Grid.Column="0"/>
<TextBlock Text="{Binding Name}" Grid.Column="1" />
<ToggleButton x:Name="Expander"
Grid.Column="2"
IsChecked="{Binding Path=IsExpanded, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}"
ClickMode="Press"/>
</Grid>
</Border>
<DataTemplate.Triggers>
<DataTrigger Binding="{Binding Path=HasItems, RelativeSource={RelativeSource AncestorType={x:Type TreeViewItem}}}" Value="False">
<Setter TargetName="Expander" Property="Visibility" Value="Hidden"/>
</DataTrigger>
</DataTemplate.Triggers>
</DataTemplate>
<GridViewColumnCollection x:Key="gvcc">
<GridViewColumn CellTemplate="{StaticResource CellTemplate}" >
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="First Column" />
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
<GridViewColumn>
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Second Column" />
</DataTemplate>
</GridViewColumn.HeaderTemplate>
<GridViewColumn.CellTemplate>
<DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn >
<GridViewColumn.HeaderTemplate>
<DataTemplate>
<TextBlock Text="Third Column"/>
</DataTemplate>
</GridViewColumn.HeaderTemplate>
</GridViewColumn>
</GridViewColumnCollection>
<Style TargetType="{x:Type TreeView}">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="{x:Type TreeView}">
<Border BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}">
<DockPanel>
<GridViewHeaderRowPresenter Columns="{StaticResource gvcc}"
ColumnHeaderContainerStyle="{StaticResource TreeHeaderStyle}"
Margin="0"
DockPanel.Dock="Top"/>
<ItemsPresenter/>
</DockPanel>
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
谢谢, 李