如果父节点未选中,我想取消选中子节点。根据我的代码,如果我检查了子节点父节点被选中。这是写入方式,但是当我取消选中父节点时,子节点仍然处于选中状态。我在 AfterCheck 事件中做了一些以下代码。
private bool updatingTreeView;
private void treSelector_AfterCheck(object sender, TreeViewEventArgs e)
{
if (updatingTreeView) return;
updatingTreeView = true;
SelectParents(e.Node, e.Node.Checked);
updatingTreeView = false;
}
private void SelectParents(TreeNode node, Boolean isChecked)
{
var parent = node.Parent;
if (parent == null)
{
//CheckAllChildren(treSelector.Nodes, false);
return;
}
if (isChecked)
{
parent.Checked = true; // we should always check parent
SelectParents(parent, true);
}
else
{
if (parent.Nodes.Cast<TreeNode>().Any(n => n.Checked))
return; // do not uncheck parent if there other checked nodes
SelectParents(parent, false);
}
}
如何解决这个问题?