3
votes

We create a HierarchicalDataTemplate for a treeview control. We can use mouse to click the tree item to change the selection. Now, we want to use keyboard up and down key to move the selection up and down. But it seems that it can't work. I searched a lot by Google and Stackoverflow, but failed.

So I created a new thread for this, could you please give me some help? thx.

<HierarchicalDataTemplate x:Uid="HierarchicalDataTemplate_1" x:Key="My_data_template" >
    <ContentControl x:Uid="ContentControl_1" MouseDoubleClick="MouseDoubleClick" MouseRightButtonDown="MouseRightClick">
        <Grid x:Uid="Grid_2" Background="Transparent">
            <Grid.ColumnDefinitions>
                <ColumnDefinition x:Uid="ColumnDefinition_1" Width="*"/>
                <ColumnDefinition x:Uid="ColumnDefinition_2" Width="Auto"/>
            </Grid.ColumnDefinitions>
            <StackPanel x:Uid="StackPanel_3" HorizontalAlignment="Left" Orientation="Horizontal" Grid.Column="0">
                <TextBlock x:Uid="TextBlock_13" Text="{Binding Name}" VerticalAlignment="Center" Margin="3,0,0,1" TextWrapping="NoWrap"/>
            </StackPanel>
            <CheckBox x:Uid="CheckBox_3" HorizontalAlignment="Right" Click="CheckBox_Click" Grid.Column="1" ToolTip="On/Off">
            </CheckBox>
        </Grid>
    </ContentControl>
</HierarchicalDataTemplate>

Another question is that, I can use mouse to click the textblock to select the item, but when I use mouse click the CheckBox, the item can't be selected. Is there anyway to make treeview item selected when I click the CheckBox?

The way I applied the template to treeview is as following:

<TreeView   x:Name="tv_pointcloud" x:Uid="TreeListView_1" 
    ItemTemplateSelector="{StaticResource DataAccessor}" 
    ......
/>

public class DataAccessor : DataTemplateSelector
{
    public DataAccessor()
    {
        Init();
    }

    public override DataTemplate SelectTemplate(object item, DependencyObject container)
    {
        var element = container as FrameworkElement;
        var template = element.FindResource("My_data_template") as DataTemplate;
        return template;
    }

    ......
}

thanks.

2
How are you binding to the tree view?Gayot Fow
Of course, and it works fine. The left problems we met are just what I said above.Orionpax
@GarryVass, I updated the information.Orionpax

2 Answers

9
votes

I had the same problem as you, in a WPF treeview I was unable to use Arrow Keys to navigate. The problem I found was the Checkbox that was getting the focus. So I set "Focusable = False for the checkbox, and now the navigation in the treeview is as expected:

<CheckBox Focusable="False" ... />

0
votes

Keyboard commands and such are called gestures. Perhaps this is a good place to get you started:

Keyboard shortcuts in WPF