下面的示例展示了如何使用 节点迭代器 获取位于指定嵌套层级上的节点数目。 在本示例中,创建了一个用于计算节点数目的 CustomNodeOperation 对象。 这个操作类包含一个内部计数器,每次当位于指定嵌套层级上的节点被访问时,此计数器都会被递增 1。
为了获取位于指定嵌套层级上的节点总数,创建了 CustomNodeOperation 类的一个实例,并传递给 TreeListNodesIterator.DoLocalOperation 方法。 在此方法执行完毕之后,读取 CustomNodeOperation.NodeCount 属性来获得节点数目。
C# | 复制代码 |
---|---|
using DevExpress.XtraTreeList.Nodes; using DevExpress.XtraTreeList.Nodes.Operations; // Declaring the custom operation class. class CustomNodeOperation : TreeListOperation { int level; int nodeCount; public CustomNodeOperation(int level) : base() { this.level = level; this.nodeCount = 0; } public override void Execute(TreeListNode node) { if(node.Level == level) nodeCount++; } public int NodeCount { get { return nodeCount; } } } // ... CustomNodeOperation operation = new CustomNodeOperation(2); treeList1.NodesIterator.DoLocalOperation(operation, treeList1.Nodes); int totalNodesAtSecondLevel = operation.NodeCount; |
Visual Basic | 复制代码 |
---|---|
Imports DevExpress.XtraTreeList.Nodes Imports DevExpress.XtraTreeList.Nodes.Operations ' Declaring the custom operation class. Class CustomNodeOperation Inherits TreeListOperation Private level As Integer Private nodeCount As Integer Public Sub New(ByVal level As Integer) Me.level = level Me.nodeCount = 0 End Sub 'New Public Overrides Sub Execute(ByVal node As TreeListNode) If node.Level = level Then nodeCount += 1 End If End Sub 'Execute Public ReadOnly Property NodeCount() As Integer Get Return nodeCount End Get End Property End Class '... Dim operation As CustomNodeOperation = New CustomNodeOperation(2) TreeList1.NodesIterator.DoLocalOperation(operation, TreeList1.Nodes) Dim totalNodesAtSecondLevel As Integer = operation.NodeCount |