【问题标题】:How To Include Checkboxes for Specific TreeView Nodes?如何包含特定 TreeView 节点的复选框?
【发布时间】:2020-09-02 10:04:03
【问题描述】:

我正在尝试创建如下所示的父树节点和子树节点,但希望仅针对 (CheckBox) 指示的节点显示 CheckBox。如果我将 TreeView 控件的 CheckBoxes 属性设置为 true,它将显示所有 TreeView 节点的 CheckBoxes。

-> Parent Node 1 (No Checkbox)
----> Child Node of Parent 1 (No Checkbox)
--------> Subnode 1 of Child Node of Parent 1 (Checkbox)
--------> Subnode 2 of Child Node of Parent 1 (Checkbox)
--------> Subnode 3 of Child Node of Parent 1 (Checkbox)
--------> Subnode 4 of Child Node of Parent 1 (Checkbox)
--------> Subnode 5 of Child Node of Parent 1 (Checkbox)

-> Parent Node 2 (No Checkbox)
----> Child Node of Parent 2 (No Checkbox)
--------> Subnode 1 of Child Node of Parent 2 (Checkbox)
--------> Subnode 2 of Child Node of Parent 2 (Checkbox)
--------> Subnode 3 of Child Node of Parent 2 (Checkbox)
--------> Subnode 4 of Child Node of Parent 2 (Checkbox)
--------> Subnode 5 of Child Node of Parent 2 (Checkbox)

【问题讨论】:

  • 哪个 TreeView 控件(Windows 窗体和 WPF 都有一个名为 TreeView 的控件 - 可能还有其他控件)。您的规则是“仅叶节点上的复选框”
  • Windows 窗体。不确定叶节点是什么,但可能答案是肯定的。
  • 您应该使用 Windows 窗体标记您的问题。下面答案中的 XAML(关于 HierarchicalDataTemplate)是关于 WPF 的。一棵树有树枝。每个分支可以有其他分支或叶子。但是一片叶子不能有任何分支。叶节点没有子节点

标签: c# winforms treeview


【解决方案1】:

您可以使用HierarchicalDataTemplate 标签为TreeView 的每个级别创建自定义数据模板。

<TreeView ItemsSource="{Binding Parents}">
    <TreeView.Resources>
        <HierarchicalDataTemplate DataType="{x:Type namespace:Parent}" ItemsSource="{Binding Children}">
            <TextBlock Text="{Binding Name}"/>
            <HierarchicalDataTemplate.ItemTemplate>
                <HierarchicalDataTemplate DataType="{x:Type namespace:child}" ItemsSource="{Binding subnodes}">
                    <TextBlock Text="{Binding name}" />
                    <HierarchicalDataTemplate.ItemTemplate>
                        <DataTemplate>
                            <StackPanel Orientation="Horizontal">
                                <CheckBox />
                                <TextBlock Text="{Binding Name}" />
                            </StackPanel>
                        </DataTemplate>
                    </HierarchicalDataTemplate.ItemTemplate>
                </HierarchicalDataTemplate>
            </HierarchicalDataTemplate.ItemTemplate>
        </HierarchicalDataTemplate>
    </TreeView.Resources>
</TreeView>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2010-10-16
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 2012-07-21
    • 2011-06-17
    • 2023-04-10
    • 2014-01-02
    相关资源
    最近更新 更多