SoFunction
Updated on 2025-04-06

DevExpress implements TreeList upward recursively to obtain parent node that meets the criteria

This article shows the method of DevExpress to implement TreeList upward recursively obtaining parent nodes that meet the conditions. It is of practical value in some project development. The specific implementation method is as follows:

The main function codes are as follows:

/// <summary>
/// Upward recursion to get the parent node that meets the criteria/// </summary>
/// <param name="node">Node that needs to be recursive upward</param>/// <param name="conditionHanlder">Judgement conditions [Termination]</param>/// <returns>Node that meets the criteria【TreeListNode】</returns>public static TreeListNode GetParentNode(this TreeListNode node, Predicate&lt;TreeListNode&gt; conditionHanlder)
{
  TreeListNode _parentNode = ;//Get the previous parent node  TreeListNode _conditonNode = null;
  if (_parentNode != null)
  {
 if (conditionHanlder(_parentNode))//Judge whether the previous parent node meets the requirements {
   _conditonNode = _parentNode;
 }
 if (_conditonNode == null)//If no node that meets the requirements is found, continue recursively   _conditonNode = GetParentNode(_parentNode, conditionHanlder);
  }
  return _conditonNode;
}

The code usage method is as follows:

TreeListNode _node = ;
TreeListNode _condionParent = _node.GetParentNode(n =&gt; () == );//Get the parent node of type CAB type(_condionParent.GetName());