Skip to main content

TreeListOperation Class

Serves as the base for classes specifying operations performed on nodes.

Namespace: DevExpress.XtraTreeList.Nodes.Operations

Assembly: DevExpress.XtraTreeList.v24.1.dll

NuGet Packages: DevExpress.Win.Navigation, DevExpress.Win.TreeList

Declaration

public abstract class TreeListOperation

Remarks

The Tree List control enables you to traverse through nodes without writing recursive code. This can be done by calling methods of the TreeListNodesIterator class that is returned by the TreeList.NodesIterator property. Methods of this class require a TreeListOperation descendant to be transmitted to them as a parameter. This parameter specifies the operation performed on each visited node. Moreover, it can define which nodes from the predefined set are to be visited.

The TreeListOperation class is abstract. You must derive a custom class from it that will represent the desired operation. The examples of operations may be: calculating the number of nodes, calculating the sum of node values from the specified field, expanding each visited node, printing visited nodes.

The logic of the operation represented by a TreeListOperation descendant must be implemented in the TreeListOperation.Execute method. This method is automatically called for each visited node. The processed node is transmitted to it as the parameter. Thus, you can perform a specific operation on the current node (for instance, collapse it) or obtain the value of the predefined field to perform desired calculations.

The most simple example of a TreeListOperation descendant is an operation that calculates the number of visited nodes. This is used by the Tree List control internally to return the TreeList.AllNodesCount property value. In such a case, the TreeListOperation.Execute method, must simply increment an internal counter.

You can also override the TreeListOperation.FinalizeOperation method when performing calculations over nodes. This method is called after all nodes have been processed. Thus you can analyse the result and modify it, if needed.

Example

The following sample code shows you how to create a custom operation class. It is used to calculate the maximum length of cell values within the specified column. The created TreeListMaxLengthOperation class is derived from the TreeListOperation class for this purpose.

The TreeListNodesIterator.DoOperation method is used to perform the operation. The result of calculations is written into the result variable.

public class TreeListMaxLengthOperation : TreeListOperation {
   private string fieldName;
   private int maxLength;

   public TreeListMaxLengthOperation(string fieldName) {
      this.fieldName = fieldName;
      maxLength = 0;
   }
   public override void Execute(TreeListNode node) {
      string nodeText = node[fieldName].ToString();
      if (nodeText.Length > maxLength) maxLength = nodeText.Length;
   }
   public int MaxLength {
      get { return maxLength; }
   }
}

// ...
// performing the declared operation and storing its result
TreeListMaxLengthOperation operation = new TreeListMaxLengthOperation("Department");
treeList1.NodesIterator.DoOperation(operation);
int result = operation.MaxLength;

Inheritance

See Also