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>

About erictummers

My work as a recruited developer changes almost every month. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s