本文主要是介绍treeview 父子节点联动,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
#region treeview联动方法private void UpdateCheckStatus(TreeViewEventArgs e){CheckAllChildNodes(e.Node);UpdateAllParentNodes(e.Node);}// updates all parent nodes recursively.private void UpdateAllParentNodes(TreeNode treeNode){TreeNode parent = treeNode.Parent;if (parent != null){if (parent.Checked && !treeNode.Checked){parent.Checked = false;UpdateAllParentNodes(parent);}else if (!parent.Checked && treeNode.Checked){bool all = true;foreach (TreeNode node in parent.Nodes){if (!node.Checked){all = false;break;}}if (all){parent.Checked = true;UpdateAllParentNodes(parent);}}}}// updates all child tree nodes recursively.private void CheckAllChildNodes(TreeNode treeNode){foreach (TreeNode node in treeNode.Nodes){node.Checked = treeNode.Checked;if (node.Nodes.Count > 0){// If the current node has child nodes, call the CheckAllChildsNodes method recursively.this.CheckAllChildNodes(node);}}}#endregion#region treeview上下级联动事件private void tvDevc_AfterCheck(object sender, TreeViewEventArgs e){if (e.Action != TreeViewAction.Unknown){UpdateCheckStatus(e);}}#endregion
这篇关于treeview 父子节点联动的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!