【问题标题】:Strange selection behavior of TreeView nodesTreeView 节点的奇怪选择行为
【发布时间】:2014-11-23 05:46:47
【问题描述】:

单击我的TreeView 的任何节点都不会选择它。我需要单击节点左侧的一个小矩形区域(在图像中显示为红色)以选择它。此外,以这种方式选择节点意味着将立即选择具有所有子节点的整个子树。不知道发生了什么。

这是我的 XAML:

<TreeView ItemsSource="{Binding FirstGeneration}">
    <TreeView.Resources>         
      <DataTemplate DataType="{x:Type local:AccountNodeVM}">
        <TreeViewItem Header ="{Binding Name}" />
      </DataTemplate>  

      <HierarchicalDataTemplate DataType="{x:Type local:CategoryNodeVM}" ItemsSource="{Binding SubCategories}">
        <TreeViewItem  Header="{Binding Name}" ItemsSource="{Binding Accounts}" />
      </HierarchicalDataTemplate>
    </TreeView.Resources>

    <TreeView.ItemContainerStyle>
      <Style TargetType="{x:Type TreeViewItem}">
        <Setter Property="IsExpanded" Value="{Binding IsExpanded, Mode=TwoWay}" />
        <Setter Property="IsSelected" Value="{Binding IsSelected, Mode=TwoWay}" />
        <Setter Property="FontWeight" Value="Normal" />
      </Style>
    </TreeView.ItemContainerStyle>
  </TreeView>

FirstGenerationSubCategoriesAccounts 都在我的 ViewModel 中定义。我也尝试在我的HierarchicalDataTemplate 中使用TextBlock 而不是TreeViewItem,但没有任何收获。我在这里做错了什么?

【问题讨论】:

    标签: c# .net wpf treeview


    【解决方案1】:

    想通了。 TreeView 似乎为每个节点自己创建一个TreeViewItem,因此在创建HierarchicalDataTemplate 时,您不需要在其中嵌入TreeViewItem,否则WPF 会将您的TreeViewItem 包装在它自己的TreeViewItem 中,从而混淆了选择机制。只需在您的HierarchicalDataTemplate 中放置一个TextBlock(或者可能是StackPanel 或其他容器,如果您有复杂的节点结构),然后让WPF 完成节点创建工作。希望这对其他人有帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-04-14
      相关资源
      最近更新 更多