wpf TreeViewItem 不可选择

wpf TreeViewItem is not selectable

我使用以下代码创建了一个 TreeView 用户控件:

<UserControl x:Class="Company.ObjectInTreeView"
         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:local="clr-namespace:Company"
         mc:Ignorable="d" 
         d:DesignHeight="450" d:DesignWidth="800"
         x:Name="ObjectInTreeViewControl">
<TreeView ItemsSource="{Binding TreeNodes, ElementName=ObjectInTreeViewControl}" SelectedItemChanged="TreeView_SelectedItemChanged">
    <TreeView.ItemContainerStyle>
        <Style TargetType="{x:Type TreeViewItem}">
            <Setter Property="ToolTip" Value="{Binding ToolTipText}"/>
            <Setter Property="Background" Value="Transparent"/>
        </Style>
    </TreeView.ItemContainerStyle>
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Path=Children}">
            <TreeViewItem IsSelected="{Binding IsSelected, Mode=TwoWay}" IsExpanded="{Binding IsExpanded, Mode=TwoWay}" Selected="TreeViewItem_Selected" Expanded="TreeViewItem_Expanded" MouseLeftButtonUp="TreeViewItem_MouseLeftButtonUp">
                <TreeViewItem.Header>
                    <StackPanel Orientation="Horizontal">
                        <TextBlock Text="{Binding Path=Value}" />
                    </StackPanel>
                </TreeViewItem.Header>
            </TreeViewItem>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

只有空 space 的 TreeViewItem 是 select 可用的,用户不能 select 通过单击项目的文本来

只有黄色部分可以点击并且select可以:

有没有人帮我找出并改正我的错误?

我找到问题了!

我只把TreeView.Resources改成这样:

<TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type local:TreeNode}" ItemsSource="{Binding Path=Children}">
            <Grid>
                <TextBlock Text="{Binding Path=Value}"/>
                <TreeViewItem Selected="TreeViewItem_Selected" Expanded="TreeViewItem_Expanded" MouseLeftButtonUp="TreeViewItem_MouseLeftButtonUp"></TreeViewItem>
            </Grid>
        </HierarchicalDataTemplate>
    </TreeView.Resources>

祝你好运