在C#TreeView中取消/检查行为

时间:2017-07-05 13:42:43

标签: c# checkbox treeview

我在树视图中找到了很多树视图和复选框......

但有一件事似乎没有被讨论。

我的树视图就像

[x] Foo
 L [x] One
 L [x] Two
 L [x] Three
[x] Bar
 L [ ] One
 L [x] Two
 L [x] Three
[ ] Hello
 L [ ] One
 L [ ] Two
 L [ ] Three

现在,当我查看我的父级复选框时,例如Foo比所有孩子都被选中。同样,取消选中Foo即可取消选中。要做到这一点,我有这个方法

private void TreeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
    foreach (TreeNode childNode in e.Node.Nodes)
    {
        childNode.Checked = e.Node.Checked;
    }
}

我使用count和其他人尝试了很多但是无法实现我想要实现的目标。

所以我想要的是,当我在TreeNode Two中检查Hello时,应该检查Hello。不检查OneThree。选中/取消选中Hello应选择/取消选择所有孩子。

这甚至可能吗?

因为当Hello被检查时,它会触发TreeView1_AfterCheck - 事件!什么实际上检查/取消所有孩子。

我的大部分尝试都以无限循环结束。

所以我的问题实际上是:

我可以实现上述行为吗?

问我有什么不清楚的地方。

1 个答案:

答案 0 :(得分:2)

我做了一个类似的应用程序,我必须做同样的事情,你想要实现。它在VB.Net中。这是片段。如果确实有帮助,请接受此答案,如果您无法理解其任何部分,请进一步评论。

VB.NET:

Private isChildCheck As Boolean = False
Private Sub TREE_VIEW_AfterCheck(sender As Object, e As TreeViewEventArgs)
    If e.Action <> TreeViewAction.Unknown Then   ' The code only executes if the user caused the checked state to change.
        If e.Node.Nodes.Count > 0 Then
            If isChildCheck Then
                isChildCheck = False
                Exit Sub 'This will check the parent to exit AfterCheck loop
            End If
            ' Calls the CheckAllChildNodes method, passing in the current 
            ' checked value of the TreeNode whose checked state changed. 
            CheckAllChildNodes(e.Node, e.Node.Checked)
        Else
            e.Node.Parent.Checked = True
            isChildCheck = True
        End If
    End If
End Sub   'After a tree node's Checked property is changed, all its child nodes are updated to the same value.

Private Sub CheckAllChildNodes(treeNode As TreeNode, nodeChecked As Boolean)

    For Each node As TreeNode In treeNode.Nodes
        node.Checked = nodeChecked

        If node.Nodes.Count > 0 Then
            ' If the current node has child nodes, call the CheckAllChildsNodes method recursively.
            CheckAllChildNodes(node, nodeChecked)
        End If
    Next node
End Sub  'Checks the childnodes of a node recursively

C#.NET(在清除需要它的人的行为变化后由Dwza编辑

private bool isChildCheck = false;
private void TREE_VIEW_AfterCheck(object sender, TreeViewEventArgs e)
{
    // The code only executes if the user caused the checked state to change.
    if (e.Action != TreeViewAction.Unknown)
    {
        if (e.Node.Nodes.Count > 0)
        {
            if (isChildCheck)
            {
                isChildCheck = false;
                return;
               //This will check the parent to exit AfterCheck loop
            }
            // Calls the CheckAllChildNodes method, passing in the current 
            // checked value of the TreeNode whose checked state changed. 
            CheckAllChildNodes(e.Node, e.Node.Checked);
        }
        else
        {
            e.Node.Parent.Checked = GetCheckStateOfChildNodes(e.Node.Parent);
            isChildCheck = !e.Node.Parent.Checked;
        }
    }
}
//After a tree node's Checked property is changed, all its child nodes are updated to the same value.

private void CheckAllChildNodes(TreeNode treeNode, bool nodeChecked)
{
    foreach (TreeNode node in treeNode.Nodes)
    {
        node.Checked = nodeChecked;
        if (node.Nodes.Count > 0)
        {
            // If the current node has child nodes, call the CheckAllChildsNodes method recursively.
            CheckAllChildNodes(node, nodeChecked);
        }
    }
} //Checks the childnodes of a node recursively

//Additional Method to react on behavior when all childs are checked/unchecked
//This part was added by dwza and is not in the VB part
private bool GetCheckStateOfChildNodes(TreeNode treeNode)
{
    foreach(TreeNode node in treeNode.Nodes)
    {
        if (node.Checked)
        {
            return true;
        }
    }
    return false;
}

希望这种转换能为您提供帮助。(使用CodeConverter转换)

相关问题