树控件一些操作

勾选后触发

  private void tV_TestItems_AfterCheck(object sender, TreeViewEventArgs e)
        {
UpdateCheckStatus(e);
}

        private void UpdateCheckStatus(TreeViewEventArgs e)
        {
            this.CheckAllChildNodes(e.Node);
            this.UpdateAllParentNodes(e.Node);
        }
        private void UpdateAllParentNodes(TreeNode treeNode)
        {
            TreeNode parent = treeNode.Parent;
            if (parent != null)
            {
                if (parent.Checked && !treeNode.Checked)
                {
                    parent.Checked = false;
                    this.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;
                        this.UpdateAllParentNodes(parent);
                    }
                }
            }
        }
        private void CheckAllChildNodes(TreeNode treeNode)
        {
            foreach (TreeNode tn in treeNode.Nodes)
            {
                tn.Checked = treeNode.Checked;
                if (tn.Nodes.Count > 0)
                    this.CheckAllChildNodes(tn);
            }
        }
View Code

 

posted @ 2019-12-18 16:41  白色風車  阅读(111)  评论(0编辑  收藏  举报