[DevExpress][TreeList]节点互斥

时间:2022-08-31 05:46:07

关键代码:

        /// <summary>
        /// 节点互斥同步
        /// 说明
        /// eg:
        ///TreeListNode _node = e.Node;
        ///_node.SyncMutexNodeCheckState(_node.CheckState, n => n.GetNodeType() == NodeType.Cab);
        /// </summary>
        /// <param name="node">需要互斥同步的节点</param>
        /// <param name="checkState">节点状态</param>
        /// <param name="checkHanlder">互斥条件【委托】</param>
        public static void SyncMutexNodeCheckState(this TreeListNode node, CheckState checkState, Predicate<TreeListNode> checkHanlder)
        {
            TreeList _tree = node.TreeList;
            if (checkHanlder(node))//当前节点符合互斥条件时候
            {
                _tree.DownRecursiveTree(n => n.CheckState = CheckState.Unchecked);
            }
            else
            {
                TreeListNode _curParentNode = node.GetParentNode(checkHanlder);//获取符合互斥条件的父节点
                if (_curParentNode == null) return;
                TreeListNode _thePubleNode = node.GetPublicParentNode(checkHanlder);//获取符合互斥条件的公共父节点
                if (_thePubleNode == null) return;
                foreach (TreeListNode n in _thePubleNode.Nodes)
                {
                    foreach (TreeListNode nc in n.Nodes)
                    {
                        if (nc != _curParentNode)
                        {
                            nc.CheckState = CheckState.Unchecked;
                            nc.DownRecursiveNode(nr => nr.CheckState = CheckState.Unchecked);
                        }
                    }
                }
            }
            node.SyncNodeCheckState(checkState);
            node.CheckState = checkState;
        }
    }
        /// <summary>
        /// 向上递归,获取符合条件的节点的公共父节点
        /// </summary>
        /// <param name="node">操作节点</param>
        /// <param name="checkHanlder">委托</param>
        /// <returns>符合条件的节点</returns>
        public static TreeListNode GetPublicParentNode(this TreeListNode node, Predicate<TreeListNode> checkHanlder)
        {
            TreeListNode _publicPNode = null;
            TreeListNode _findNode = node.GetParentNode(checkHanlder);//先获取到条件判断的自身父节点
            if (_findNode != null)
            {
                //开始向上递归
                UpwardRecursiveNode(_findNode, n =>
                {
                    TreeListNode _curpublicNode = n.ParentNode;//获取当前向上递归的父节点
                    if (_curpublicNode != null)
                    {
                        if (_curpublicNode.Nodes.Count > 1)//若有多个子节点,则是公共父节点
                        {
                            _publicPNode = _curpublicNode;
                            return false;//跳出递归
                        }
                    }
                    return true;//继续递归
                });
            }
            return _publicPNode;
        }

        /// <summary>
        /// 向上递归,获取符合条件的父节点
        /// </summary>
        /// <param name="node">需要向上递归的节点</param>
        /// <param name="conditionHanlder">判断条件【委托】</param>
        /// <returns>符合条件的节点【TreeListNode】</returns>
        public static TreeListNode GetParentNode(this TreeListNode node, Predicate<TreeListNode> conditionHanlder)
        {
            TreeListNode _parentNode = node.ParentNode;//获取上一级父节点
            TreeListNode _conditonNode = null;
            if (_parentNode != null)
            {
                if (conditionHanlder(_parentNode))//判断上一级父节点是否符合要求
                {
                    _conditonNode = _parentNode;
                }
                if (_conditonNode == null)//若没有找到符合要求的节点,递归继续
                    _conditonNode = GetParentNode(_parentNode, conditionHanlder);
            }
            return _conditonNode;
        }
SyncNodeCheckState代码参考:http://www.cnblogs.com/Yan-Zhiwei/p/3808089.html
说明:
[DevExpress][TreeList]节点互斥
如图,Test3和蒙自路Test2都是"cab"类型;
当调用代码如下:
            TreeListNode _node = e.Node;
            _node.SyncMutexNodeCheckState(_node.CheckState, n => n.GetNodeType() == NodeType.Cab);

实现的效果就是要么只能勾选“Test3”或者“蒙自路Test2”节点或者子节点,不同同时勾选,应该就是互斥的意思;也是这段代码想实现的效果,希望有所帮助。