DevExpress完成TreeList向上遞歸獲得公共父節點的辦法。本站提示廣大學習愛好者:(DevExpress完成TreeList向上遞歸獲得公共父節點的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是DevExpress完成TreeList向上遞歸獲得公共父節點的辦法正文
有時刻在停止C#項目開辟中,須要獲得到公共節點,以下圖所示:

比方,當點擊“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;
}
願望本文所述示例對年夜家停止相似的C#項目開辟能有所贊助!