首页 > 代码库 > [WinForm][DevExpress][TreeList]向上递归,获取公共父节点

[WinForm][DevExpress][TreeList]向上递归,获取公共父节点

最近项目开发中,需要获取到公共节点,如图:

image

譬如,当点击“Test103-2”节点,其类型是“灯”类型,那怎么获取到“中心区域”这个类型是“地域”的公共节点了?(不知道描述清楚木有哈)

核心代码:

        /// <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;        }        /// <summary>        /// 向上递归节点        /// </summary>        /// <param name="node">需要向上递归的节点</param>        /// <param name="conditionHanlder">委托,返回fasle跳出递归;返回true继续递归;</param>        public static void UpwardRecursiveNode(this TreeListNode node, Predicate<TreeListNode> conditionHanlder)        {            TreeListNode _parentNode = node.ParentNode;            if (_parentNode != null)            {                if (conditionHanlder(_parentNode))                {                    UpwardRecursiveNode(_parentNode, conditionHanlder);                }            }        }        /// <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;        }
<style type="text/css">.csharpcode, .csharpcode pre{ font-size: small; color: black; font-family: consolas, "Courier New", courier, monospace; background-color: #ffffff; /*white-space: pre;*/}.csharpcode pre { margin: 0em; }.csharpcode .rem { color: #008000; }.csharpcode .kwrd { color: #0000ff; }.csharpcode .str { color: #006080; }.csharpcode .op { color: #0000c0; }.csharpcode .preproc { color: #cc6633; }.csharpcode .asp { background-color: #ffff00; }.csharpcode .html { color: #800000; }.csharpcode .attr { color: #ff0000; }.csharpcode .alt { background-color: #f4f4f4; width: 100%; margin: 0em;}.csharpcode .lnum { color: #606060; }</style>希望有所帮助!