private void tvSysFunctionMenu_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
{
if (e.Node != tvSysFunctionMenu.Nodes[0])
{
SetChildNodeChecked(e.Node);
SetParentNodeChecked(e.Node);
}
else
{
SetChildNodeChecked(e.Node);
}
}
//根据父节点状态设置子节点的状态
private void SetChildNodeChecked(TreeNode parentNode)
{
foreach (TreeNode node in parentNode.Nodes)
{
node.Checked = parentNode.Checked;
if (node.Nodes.Count > 0)
{
SetChildNodeChecked(node);
}
}
}
//根据子节点状态设置父节点的状态
private void SetParentNodeChecked(TreeNode childNode)
{
TreeNode parentNode = childNode.Parent;
if (!parentNode.Checked && childNode.Checked) //当子结点选中,父结点没选中时,则:选中父结点
{
parentNode.Checked = true;
}
else if (parentNode.Checked && !childNode.Checked) //取消选中其下全部子结点时,其父结点也取消
{
int checks = 0; //统计子结点的个数(为0时表示取消了其下全部子结点)
foreach (TreeNode node in parentNode.Nodes)
{
if (node.Checked)
{
checks++;
break;
}
}
if (checks == 0) //子结点都取消选中时,其父结点也取消
{
parentNode.Checked = false;
if (!parentNode.Index.Equals(0))
{
SetParentNodeChecked(parentNode);
}
}
}
}
//节点选中状态更改
protected void TreeNodeCheckStatusChanged(object sender, TreeNodeMouseClickEventArgs e)
{
SetChildNodeChecked(e.Node);
// 判断是否是根节点
if (!e.Node.Index.Equals(0))
{
SetParentNodeChecked(e.Node);
}
}
{
if (e.Node != tvSysFunctionMenu.Nodes[0])
{
SetChildNodeChecked(e.Node);
SetParentNodeChecked(e.Node);
}
else
{
SetChildNodeChecked(e.Node);
}
}
//根据父节点状态设置子节点的状态
private void SetChildNodeChecked(TreeNode parentNode)
{
foreach (TreeNode node in parentNode.Nodes)
{
node.Checked = parentNode.Checked;
if (node.Nodes.Count > 0)
{
SetChildNodeChecked(node);
}
}
}
//根据子节点状态设置父节点的状态
private void SetParentNodeChecked(TreeNode childNode)
{
TreeNode parentNode = childNode.Parent;
if (!parentNode.Checked && childNode.Checked) //当子结点选中,父结点没选中时,则:选中父结点
{
parentNode.Checked = true;
}
else if (parentNode.Checked && !childNode.Checked) //取消选中其下全部子结点时,其父结点也取消
{
int checks = 0; //统计子结点的个数(为0时表示取消了其下全部子结点)
foreach (TreeNode node in parentNode.Nodes)
{
if (node.Checked)
{
checks++;
break;
}
}
if (checks == 0) //子结点都取消选中时,其父结点也取消
{
parentNode.Checked = false;
if (!parentNode.Index.Equals(0))
{
SetParentNodeChecked(parentNode);
}
}
}
}
//节点选中状态更改
protected void TreeNodeCheckStatusChanged(object sender, TreeNodeMouseClickEventArgs e)
{
SetChildNodeChecked(e.Node);
// 判断是否是根节点
if (!e.Node.Index.Equals(0))
{
SetParentNodeChecked(e.Node);
}
}
private bool updateTreeNode = false;
void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (updateTreeNode) return; // 如果在刷新中
updateTreeNode = true;
try
{
SetChildTreeNodeChecked(e.Node, e.Node.Checked); // 同步子节点
SetParentTreeNodeChecked(e.Node, e.Node.Checked);
}
finally
{
updateTreeNode = false;
}
}
private void SetChildTreeNodeChecked(TreeNode currentTreeNode, bool isChecked) // 同步子节点
{
if (currentTreeNode == null) return;
currentTreeNode.Checked = isChecked;
foreach (TreeNode treeNode in currentTreeNode.Nodes)
SetChildTreeNodeChecked(treeNode, isChecked);
}
private void SetParentTreeNodeChecked(TreeNode currentTreeNode, bool isChecked)
{
if (currentTreeNode == null) return;
currentTreeNode.Checked = isChecked;
if (isChecked) // 如果选中本节点,则选中全部上级节点
{
SetParentTreeNodeChecked(currentTreeNode.Parent, isChecked);
}
else
{
if (currentTreeNode.Parent != null && currentTreeNode.Parent.Checked)
{
foreach (TreeNode treeNode in currentTreeNode.Parent.Nodes)
if (treeNode.Checked) return;
SetParentTreeNodeChecked(currentTreeNode.Parent, isChecked); // 判断是否所有的兄弟节点Checked都为false
}
}
}
void treeView1_AfterCheck(object sender, TreeViewEventArgs e)
{
if (updateTreeNode) return; // 如果在刷新中
updateTreeNode = true;
try
{
SetChildTreeNodeChecked(e.Node, e.Node.Checked); // 同步子节点
SetParentTreeNodeChecked(e.Node, e.Node.Checked);
}
finally
{
updateTreeNode = false;
}
}
private void SetChildTreeNodeChecked(TreeNode currentTreeNode, bool isChecked) // 同步子节点
{
if (currentTreeNode == null) return;
currentTreeNode.Checked = isChecked;
foreach (TreeNode treeNode in currentTreeNode.Nodes)
SetChildTreeNodeChecked(treeNode, isChecked);
}
private void SetParentTreeNodeChecked(TreeNode currentTreeNode, bool isChecked)
{
if (currentTreeNode == null) return;
currentTreeNode.Checked = isChecked;
if (isChecked) // 如果选中本节点,则选中全部上级节点
{
SetParentTreeNodeChecked(currentTreeNode.Parent, isChecked);
}
else
{
if (currentTreeNode.Parent != null && currentTreeNode.Parent.Checked)
{
foreach (TreeNode treeNode in currentTreeNode.Parent.Nodes)
if (treeNode.Checked) return;
SetParentTreeNodeChecked(currentTreeNode.Parent, isChecked); // 判断是否所有的兄弟节点Checked都为false
}
}
}