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.