我有一个继承自 asp.net TreeView 控件的自定义 TreeView 。具有 n 级父子关系。根据一些计算,我检查了子节点。如果检查了所有子节点,我希望应该检查父节点。因为我正在根据一些计算检查子节点,所以我不能在检查事件后使用。 有人可以为我提供 C# 代码吗?
private TreeNode _parentNode;
private void CheckedParent(TreeNodeCollection nodeCollection)
{
foreach (TreeNode node in nodeCollection)
{
if (node.ChildNodes.Count > 0)
{
_parentNode = node;
CheckedParent(node.ChildNodes);
}
else
{
bool allChildChecked = true
foreach (TreeNode childNode in nodeCollection)
{
if (!childNode.Checked)
{
allChildChecked = false;
}
}
}
}
if (allChildChecked )
{
_parentNode.Checked = true;
_isAllChildChecked = false;
}
}
最佳答案
如果所有子节点都被选中,该方法将返回true
;否则它将返回 false
private bool AllChildChecked(TreeNode currentNode)
{
bool res = true;
foreach (TreeNode node in currentNode.ChildNodes)
{
res = node.Checked;
if (!res) break;
res = this.AllChildChecked(node);
if (!res) break;
}
return res;
}
关于c# - 如果检查了所有子节点,则检查父节点 C# asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11614126/