dev treelist 常用用法小结

要求如下:
1:选择父节点后,子节点全部打钩;
2:选择子节点而不选择父节点,则从当前节点的父节点一直到根节点check框都是半选状态。
3:当子节点均未勾选时,设置父节点状态为:未选中状态
具体实现如下:
一、OptionView—ShowCheckBoxes属性为true,则每个节点前面自动增加一个check框。
二、设置OptionBehavior—AllIndeterminateCheckState属性为true,则check框可以出现半选状态。

具体代码显示如下:


 #region 树形 点击节点前
        /// <summary>  
        /// 点击节点前  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void treeList_BeforeCheckNode(object sender, CheckNodeEventArgs e)
        {
            if (!this.ifMultipleChoice)
            {
                //选择前取消前面已经选择的,实现单选功能
                List<DevExpress.XtraTreeList.Nodes.TreeListNode> treeListNodes = treeList.GetAllCheckedNodes();
                foreach (DevExpress.XtraTreeList.Nodes.TreeListNode node in treeListNodes)
                {
                    node.Checked = false;
                }
            }
            e.State = (e.PrevState == CheckState.Checked ? CheckState.Unchecked : CheckState.Checked);
        }
        #endregion

        #region 树形 点击节点后
        /// <summary>  
        /// 点击节点后  
        /// </summary>  
        /// <param name="sender"></param>  
        /// <param name="e"></param>  
        private void treeList_AfterCheckNode(object sender, NodeEventArgs e)
        {
            if (!this.ifMultipleChoice)
            {
                return;
            }
            if (e.Node == null)
            {
                return;
            }
            if (e.Node.CheckState == CheckState.Indeterminate)
            {
                e.Node.CheckState = CheckState.Checked;
            }
            if (e.Node.CheckState == CheckState.Checked)
            {
                if (e.Node.HasChildren)
                {
                    this.CheckNode(e.Node);
                }
                this.ValidParentIsChecked(e.Node);
            }
            else if (e.Node.CheckState == CheckState.Unchecked)
            {
                if (e.Node.HasChildren)
                {
                    this.SetCheckedChildNodes(e.Node, CheckState.Unchecked);
                }
                //从根节点往下 查询 是否有打钩的子节点,如果有那么 父节点的 半选状态不变 否则变为 不选择状态  
                //this.ValidParentNodeIsCanleSel(e.Node);
                SetCheckedParentNodes(e.Node, e.Node.CheckState);

            }
        }
        #endregion

        #region 树形 节点操作集合
        /// <summary>  
        /// 选择某个节点及其子节点  
        /// </summary>  
        /// <param name="node"></param>  
        private void CheckNode(DevExpress.XtraTreeList.Nodes.TreeListNode node)
        {
            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes)
            {
                if (cnode != null)
                {
                    cnode.CheckState = CheckState.Checked;
                }
                if (cnode.HasChildren)
                {
                    CheckNode(cnode);
                }
            }
        }

        /// <summary>  
        /// 勾选子节点后,修改父节点的状态  
        /// </summary>  
        /// <param name="node"></param>  
        private void ValidParentIsChecked(DevExpress.XtraTreeList.Nodes.TreeListNode node)
        {
            if (node.ParentNode != null)
            {
                //父节点未被选中  
                if (node.ParentNode.CheckState == CheckState.Unchecked)
                {
                    //该节点有子节点 并且  该节点与子节点均被选中  
                    if (node.HasChildren && ValidIsCheckAllChildNode(node.ParentNode))
                    {
                        node.ParentNode.CheckState = CheckState.Checked;
                    }
                    //该节点没有子节点 并且 其父节点下的子节点均被选中  
                    else if ((!node.HasChildren) && ValidIsCheckAllChildNode(node.ParentNode))
                    {
                        node.ParentNode.CheckState = CheckState.Checked;
                    }
                    else
                    {
                        node.ParentNode.CheckState = CheckState.Indeterminate;
                    }
                }
                else if (node.ParentNode.CheckState == CheckState.Indeterminate)
                {
                    //该节点有子节点并且 子节点均未被选中时,修改其状态   
                    if (node.HasChildren && (!this.ValidIsHasCheckChildNode(node)))
                    {
                        node.ParentNode.CheckState = CheckState.Unchecked;
                    }
                    // 该节点的父节点下所有节点均被选中 ValidIsCheckAllChildNode  
                    else if (ValidIsCheckAllChildNode(node.ParentNode))
                    {
                        node.ParentNode.CheckState = CheckState.Checked;
                    }
                }
                this.ValidParentIsChecked(node.ParentNode);
            }
        }

        /// <summary>  
        /// 从该节点往下 查询 是否有打钩的子节点,如果有那么 父节点的 半选、选中状态不变否则变为 不选择状态  
        /// </summary>  
        /// <param name="node"></param>  
        private void ValidParentNodeIsCanleSel(DevExpress.XtraTreeList.Nodes.TreeListNode node)
        {
            bool isSel = false;
            if (node.ParentNode != null)
            {
                if (node.ParentNode.CheckState == CheckState.Indeterminate)
                {
                    //如果父节点的 状态为 半选 状态 这 根据父节点 判断子节点是否打钩  
                    isSel = ValidIsHasCheckChildNode(node.ParentNode);
                    if (isSel == false)
                    {
                        //如果所有的 子节点 都没有 “选中”那么 父节点的状态 变为 非选中状态  
                        node.ParentNode.CheckState = CheckState.Unchecked;
                    }
                }
                this.ValidParentNodeIsCanleSel(node.ParentNode);
            }
        }

        /// <summary>  
        /// 判断该节点下是否有节点被选中,如果有返回true  
        /// </summary>  
        /// <param name="node"></param>  
        /// <returns></returns>  
        private bool ValidIsHasCheckChildNode(DevExpress.XtraTreeList.Nodes.TreeListNode node)
        {
            bool isCheck = false;
            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes)
            {
                if (cnode != null)
                {
                    if (cnode.CheckState == CheckState.Checked)
                    {
                        isCheck = true;
                        return isCheck;
                    }
                }
                if (cnode.HasChildren)
                {
                    isCheck = this.ValidIsHasCheckChildNode(cnode);
                    if (isCheck == true)
                    {
                        return isCheck;
                    }
                }
            }
            return isCheck;
        }
        /// <summary>  
        /// 节点全选  
        /// </summary>  
        /// <param name="node"></param>  
        /// <returns></returns>  
        private bool ValidIsCheckAllChildNode(DevExpress.XtraTreeList.Nodes.TreeListNode node)
        {
            bool isCheck = true;
            foreach (DevExpress.XtraTreeList.Nodes.TreeListNode cnode in node.Nodes)
            {
                if (cnode != null)
                {
                    if (cnode.CheckState == CheckState.Unchecked)
                    {
                        isCheck = false;
                        return isCheck;
                    }
                }
                if (cnode.HasChildren)
                {
                    isCheck = this.ValidIsCheckAllChildNode(cnode);
                    if (isCheck == false)
                    {
                        return isCheck;
                    }
                }
            }
            return isCheck;
        }


        #endregion

        #region  树形 获取全部子节点
        private void GetChildNodes(TreeListNode parentNode, List<TreeListNode> list)
        {
            if (parentNode == null)
            {
                return;
            }
            if (parentNode.Nodes.Count > 0)
            {
                foreach (TreeListNode node in parentNode.Nodes)
                {
                    list.Add(node);
                    if (node.Nodes.Count > 0)
                    {
                        GetChildNodes(node, list);
                    }
                }
            }
        }
        #endregion

        #region 树形 选择子节点时触发
        /// <summary>  
        /// 选择子节点时触发  
        /// </summary>  
        /// <param name="node"></param>  
        /// <param name="check"></param>  
        private void SetCheckedChildNodes(TreeListNode node, CheckState check)
        {
            foreach (TreeListNode tn in node.Nodes)
            {
                tn.CheckState = check;
                if (tn.HasChildren)
                {
                    SetCheckedChildNodes(tn, check);
                }
            }
        }
        #endregion

        #region  树形 选择父节点时触发
        /// <summary>  
        /// 选择父节点时触发  
        /// </summary>  
        /// <param name="node"></param>  
        /// <param name="check"></param>  
        private void SetCheckedParentNodes(TreeListNode node, CheckState check)
        {
            if (node.ParentNode != null)
            {
                bool b = false;
                CheckState state;
                foreach (TreeListNode tln in node.ParentNode.Nodes)
                {
                    state = (CheckState)tln.CheckState;
                    if (!check.Equals(state))
                    {
                        b = !b;
                        break;
                    }
                }
                node.ParentNode.CheckState = b ? CheckState.Indeterminate : check;
                SetCheckedParentNodes(node.ParentNode, check);
            }
        }
        #endregion

        #region 树形 判断此节点下的所有孩子节点是否选中
        /// <summary>  
        /// 判断此节点下的所有孩子节点是否选中  
        /// </summary>  
        /// <param name="node"></param>  
        /// <returns></returns>  
        private Boolean IsChildsChecked(TreeListNode node)
        {
            foreach (TreeListNode tln in node.ParentNode.Nodes)
            {
                if (tln.CheckState == CheckState.Unchecked)
                {
                    return false;
                }
                if (tln.HasChildren)
                {
                    IsChildsChecked(tln);
                }
            }
            return true;
        }
        #endregion
		
		



以上代码参考:点击打开链接


你可能感兴趣的:(方法,用法,小结,dev,Treelist)