0

我有一个包含一些父节点和子节点的树视图。我有四个按钮。其中两个按钮是选中和未选中的只有父节点,两个按钮是子节点。如果我单击,我只想检查父节点,如果我单击btnAllParentChecked,我只想取消选中btnAllparentUnchecked子节点。我已经为子节点做了一些代码。

Boolean bChildTrigger = true;
        Boolean bParentTrigger = true;
        private void CheckAllChildren(TreeNodeCollection trNodeCollection, Boolean bCheck)
        {
            bParentTrigger = false;
            foreach (TreeNode ctn in trNodeCollection)
            {
                bChildTrigger = false;
                ctn.Checked = bCheck;
                bChildTrigger = true;

                CheckAllChildren(ctn.Nodes, bCheck);
            }
            bParentTrigger = true;
        }

 private void btnAllPropertyChecked_Click(object sender, EventArgs e)
        {
            CheckAllChildren(treSelector.Nodes, true);
        }

private void btnAllPropertyUnChecked_Click(object sender, EventArgs e)
        {
            CheckAllChildren(treSelector.Nodes, false);
        }

如何在树视图中实现此功能?

4

1 回答 1

1

这就是你所需要的。经过测试并且运行良好

private void ChangeNodesSelection(TreeNodeCollection node,bool doCheck)
    {
        foreach (TreeNode n in node)
        {
            n.Checked = doCheck;
            if (n.Nodes.Count > 0)
            {
                ChangeNodesSelection(n.Nodes,doCheck);
            }
        }
    }

private void UncheckParentNodes(TreeNodeCollection node)
    {
        foreach (TreeNode n in node)
        {
            if (n.Parent == null && n.Nodes.Count == 0)
                n.Checked = false;
        }
    }
于 2013-08-16T11:20:43.133 回答