My project contains a treeview which is bound to a hierarchical list. The data is displayed correct, but the nodes cannot be selected by clicking the text, you must click on the little space reserved for an icon of so. Here’s my code:
<UserControl x:Class="MainDetailControl" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:my="clr-namespace:DetailProject" mc:Ignorable="d"> <UserControl.Resources> <my:TestData x:Key="testdata" /> <HierarchicalDataTemplate x:Key="hierarchicalitemtemplate" DataType="{x:Type my:Node}" ItemsSource="{Binding Path=Children}"> <TreeViewItem> <TreeViewItem.Header> <TextBlock Text="{Binding Path=Name}" /> </TreeViewItem.Header> </TreeViewItem> </HierarchicalDataTemplate> </UserControl.Resources> <Grid> <Grid.ColumnDefinitions> <ColumnDefinition/> </Grid.ColumnDefinitions> <TreeView Name="ManagerTree" ItemTemplate="{StaticResource hierarchicalitemtemplate}" ItemsSource="{Binding Path=NodeList}" Grid.Column="0" VerticalAlignment="Stretch" HorizontalAlignment="Stretch"> <TreeView.ItemContainerStyle> <Style TargetType="TreeViewItem"> <Setter Property="IsExpanded" Value="True"/> </Style> </TreeView.ItemContainerStyle> </TreeView> </Grid> </UserControl>
And here is the solution. Do not use the TreeViewItem but directly put the content in.
<HierarchicalDataTemplate x:Key="hierarchicalitemtemplate" DataType="{x:Type my:Node}" ItemsSource="{Binding Path=Children}"> <TextBlock Text="{Binding Path=Name}" /> </HierarchicalDataTemplate>
Gracias, tenia el mismo caso.