时间:2020-10-10 11:46:23 | 栏目:.NET代码 | 点击:次
本文实例讲述了DevExpress实现TreeList节点互斥的方法,具体实现方法如下所示:
主要功能代码如下:
/// <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代码可以参考:https://www.jb51.net/article/53335.htm
说明:
如上图所示,节点“Test3”和“蒙自路Test2”都是"cab"类型;
当调用代码如下:
TreeListNode _node = e.Node; _node.SyncMutexNodeCheckState(_node.CheckState, n => n.GetNodeType() == NodeType.Cab);
实现的效果就是要么只能勾选“Test3”或者“蒙自路Test2”节点或者子节点,不同同时勾选,应该就是互斥的意思;也是这段代码想实现的效果,希望对大家的项目开发有所帮助。