4

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.

Orionpax
  • 993
  • 5
  • 13
  • 27

2 Answers2

10

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" ... />

Eric T.
  • 101
  • 1
  • 4
  • 3
    Don't be discouraged by the low upvotes on here. This works. Set Focusable for all the controls in the HierarchicalDataTemplate to false. This will also make it to where you can select the entire node instead of only part of it. – Millie Smith Mar 19 '14 at 19:01
0

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

Keyboard shortcuts in WPF

Community
  • 1
  • 1
Zache
  • 1,023
  • 6
  • 14
  • Thanks for your help. Currently, the user control containing my treeview could accept up and down key press, but it seems that it can't forward it to treeview control. – Orionpax Jul 03 '13 at 07:13