C# WinForm TreeView 递归选择父节点和子节点
private void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (e.Action != TreeViewAction.ByMouse)
return;
SetParentNode(e.Node);
SetChildNode(e.Node);
}
private void SetParentNode(TreeNode node)
{
if (node.Level > 0)
{
node.Parent.Checked = true;
SetParentNode(node.Parent);
}
}
private void SetChildNode(TreeNode node)
{
foreach (TreeNode childNode in node.Nodes)
{
childNode.Checked = node.Checked;
if (node.Nodes.Count > 0)
SetChildNode(childNode);
}
}