WPF treeview node select

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>
Unknown's avatar

About erictummers

Working in a DevOps team is the best thing that happened to me. I like challenges and sharing the solutions with others. On my blog I’ll mostly post about my work, but expect an occasional home project, productivity tip and tooling review.
This entry was posted in Development and tagged , , . Bookmark the permalink.

1 Response to WPF treeview node select

  1. ALEXIE CORDOVA's avatar ALEXIE CORDOVA says:

    Gracias, tenia el mismo caso.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.