Back to Devexpress

TreeList.CreateCustomNode Event

windowsforms-devexpress-dot-xtratreelist-dot-treelist-0970b24a.md

latest5.4 KB
Original Source

TreeList.CreateCustomNode Event

Provides the ability to create custom nodes.

Namespace : DevExpress.XtraTreeList

Assembly : DevExpress.XtraTreeList.v25.2.dll

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

Declaration

csharp
public event CreateCustomNodeEventHandler CreateCustomNode
vb
Public Event CreateCustomNode As CreateCustomNodeEventHandler

Event Data

The CreateCustomNode event's data class is CreateCustomNodeEventArgs. The following properties provide information specific to this event:

PropertyDescription
NodeGets or sets a value which represents the created node.
NodeIDGets the node’s unique identifier.
OwnerGets the collection of nodes which owns the created node.
TagGets the data associated with the Tree List node via the constructor.

Remarks

The CreateCustomNode event fires when a new node is added to a Tree List. Handle this event to add custom nodes (TreeListNode descendants).

To add a custom node to the Tree List, the node should be assigned to the event parameter’s CreateCustomNodeEventArgs.Node property which represents the added node. The CreateCustomNodeEventArgs.NodeID and CreateCustomNodeEventArgs.Owner properties represent the node’s unique identifier and the collection of nodes to which the added node belongs, respectively.

Example

The following example handles the TreeList.CreateCustomNode event to add custom nodes to the Tree List. Custom nodes are represented by the CustomTLNode class which extends the functionality of the TreeListNode class. It introduces the ChildNodesCount property which returns the total number of nodes owned by the current node.

csharp
using DevExpress.XtraTreeList;
using DevExpress.XtraTreeList.Nodes;
using DevExpress.XtraTreeList.Nodes.Operations;

private void treeList1_CreateCustomNode(object sender, CreateCustomNodeEventArgs e) {
   e.Node = new CustomTLNode(e.NodeID, e.Owner);
}
// ...
public class CustomTLNode : TreeListNode {
   public CustomTLNode(int id, TreeListNodes owner) : base(id, owner) {}
   public int ChildNodesCount {
      get {
         if(this.TreeList == null) return -1;
         NodesCountOperation operation = new NodesCountOperation();
         this.TreeList.NodesIterator.DoLocalOperation(operation, this.Nodes);
         return operation.Result;
      }
   }
}
public class NodesCountOperation : TreeListOperation {
   private int result;
   public NodesCountOperation() {
      result = 0;
   }
   public override void Execute(TreeListNode node) {
      result++;
   }
   public int Result {
      get { return result; }
   }
}
vb
Imports DevExpress.XtraTreeList
Imports DevExpress.XtraTreeList.Nodes
Imports DevExpress.XtraTreeList.Nodes.Operations

Private Sub TreeList1_CreateCustomNode(ByVal sender As Object, _
ByVal e As CreateCustomNodeEventArgs) Handles TreeList1.CreateCustomNode
    e.Node = New CustomTLNode(e.NodeID, e.Owner)
End Sub
' ...
Public Class CustomTLNode
    Inherits TreeListNode

    Public Sub New(ByVal id As Integer, ByVal owner As TreeListNodes)
        MyBase.New(id, owner)
    End Sub

    Public ReadOnly Property ChildNodesCount() As Integer
        Get
            If Me.TreeList Is Nothing Then
                Return -1
            End If
            Dim operation As New NodesCountOperation()
            Me.TreeList.NodesIterator.DoLocalOperation(operation, Me.Nodes)
            Return operation.Result
        End Get
    End Property
End Class

Public Class NodesCountOperation
    Inherits TreeListOperation
    Dim _result As Integer

    Public Sub New()
        _result = 0
    End Sub

    Public Overrides Sub Execute(ByVal node As TreeListNode)
        _result += 1
    End Sub

    Public ReadOnly Property Result() As Integer
        Get
            Return _result
        End Get
    End Property
End Class

See Also

AppendNode

TreeListOperation

TreeList Class

TreeList Members

DevExpress.XtraTreeList Namespace