WPF - control keyboard navigation in TreeView
I have a problem with the keyboard navigation in my treeview. I'm implementing a 3 column table. Each element in the first column consists of 3 columns: An Icon, a textBlock and an expander. If the element's property "hasItems" is false, the expander is hidden. My problem is that I can't use the up and down arrows to navigate up and down the tree. I've tried to set the expander "IsTabStop" property to true but this doesn't give me the required result. It feels like the expander takes the focus: The up and down arrows only get the focus on and off the expander (when I remove the expander the navigation acts as expected).
This is some kind of pseudo code, just to explain my hirerchy:
<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>
开发者_Go百科 <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>
Thanks, Li
精彩评论