How to: Iterate Through Nodes via the Node Iterator
- 6 minutes to read
This example shows how to traverse through all visible nodes and expand only those nodes that have more than four child nodes. Nodes that have less than four child nodes are collapsed.
Note
A complete sample project is available at https://github.com/DevExpress-Examples/iterating-through-nodes-via-tree-traversal-api-e3256.
Imports Microsoft.VisualBasic
Imports System.Windows
Imports DevExpress.Xpf.Grid
Namespace DXTreeList_NodeTraversing
''' <summary>
''' Interaction logic for MainWindow.xaml
''' </summary>
Partial Public Class MainWindow
Inherits Window
Public Sub New()
InitializeComponent()
treeListControl1.ItemsSource = Stuff.GetStuff()
End Sub
Private Sub treeListControl1_Loaded(ByVal sender As Object, ByVal e As RoutedEventArgs)
SmartExpandNodes(4)
End Sub
Private Sub SmartExpandNodes(ByVal minChildCount As Integer)
Dim nodeIterator As New TreeListNodeIterator(treeListView1.Nodes, True)
Do While nodeIterator.MoveNext()
nodeIterator.Current.IsExpanded = nodeIterator.Current.Nodes.Count >= minChildCount
Loop
End Sub
End Class
End Namespace
<Window x:Class="DXTreeList_NodeTraversing.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525"
xmlns:sys="clr-namespace:System;assembly=mscorlib"
xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"
xmlns:dx="http://schemas.devexpress.com/winfx/2008/xaml/core">
<Grid>
<dxg:TreeListControl Name="treeListControl1" Loaded="treeListControl1_Loaded" >
<dxg:TreeListControl.Columns>
<dxg:TreeListColumn FieldName="Name" />
<dxg:TreeListColumn FieldName="Department" />
<dxg:TreeListColumn FieldName="Position" />
</dxg:TreeListControl.Columns>
<dxg:TreeListControl.View>
<dxg:TreeListView Name="treeListView1" AutoWidth="True" ShowIndicator="False"
KeyFieldName="ID" ParentFieldName="ParentID">
<!--<dxg:TreeListView.RootValue>
<sys:Int32>1</sys:Int32>
</dxg:TreeListView.RootValue>-->
</dxg:TreeListView>
</dxg:TreeListControl.View>
</dxg:TreeListControl>
</Grid>
</Window>
Imports Microsoft.VisualBasic
Imports System
Imports System.Collections.Generic
Namespace DXTreeList_NodeTraversing
Public Class Employee
Private privateID As Integer
Public Property ID() As Integer
Get
Return privateID
End Get
Set(ByVal value As Integer)
privateID = value
End Set
End Property
Private privateParentID As Integer
Public Property ParentID() As Integer
Get
Return privateParentID
End Get
Set(ByVal value As Integer)
privateParentID = value
End Set
End Property
Private privateName As String
Public Property Name() As String
Get
Return privateName
End Get
Set(ByVal value As String)
privateName = value
End Set
End Property
Private privatePosition As String
Public Property Position() As String
Get
Return privatePosition
End Get
Set(ByVal value As String)
privatePosition = value
End Set
End Property
Private privateDepartment As String
Public Property Department() As String
Get
Return privateDepartment
End Get
Set(ByVal value As String)
privateDepartment = value
End Set
End Property
End Class
Public NotInheritable Class Stuff
Private Sub New()
End Sub
Public Shared Function GetStuff() As List(Of Employee)
Dim stuff As New List(Of Employee)()
stuff.Add(New Employee() With {.ID = 1, .ParentID = 0, .Name = "Gregory S. Price", .Department = "", .Position = "President"})
stuff.Add(New Employee() With {.ID = 2, .ParentID = 1, .Name = "Irma R. Marshall", .Department = "Marketing", .Position = "Vice President"})
stuff.Add(New Employee() With {.ID = 3, .ParentID = 1, .Name = "John C. Powell", .Department = "Operations", .Position = "Vice President"})
stuff.Add(New Employee() With {.ID = 4, .ParentID = 1, .Name = "Christian P. Laclair", .Department = "Production", .Position = "Vice President"})
stuff.Add(New Employee() With {.ID = 5, .ParentID = 1, .Name = "Karen J. Kelly", .Department = "Finance", .Position = "Vice President"})
stuff.Add(New Employee() With {.ID = 6, .ParentID = 2, .Name = "Brian C. Cowling", .Department = "Marketing", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 7, .ParentID = 2, .Name = "Thomas C. Dawson", .Department = "Marketing", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 8, .ParentID = 2, .Name = "Angel M. Wilson", .Department = "Marketing", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 9, .ParentID = 2, .Name = "Bryan R. Henderson", .Department = "Marketing", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 10, .ParentID = 3, .Name = "Harold S. Brandes", .Department = "Operations", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 11, .ParentID = 3, .Name = "Michael S. Blevins", .Department = "Operations", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 12, .ParentID = 3, .Name = "Jan K. Sisk", .Department = "Operations", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 13, .ParentID = 3, .Name = "Sidney L. Holder", .Department = "Operations", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 14, .ParentID = 4, .Name = "James L. Kelsey", .Department = "Production", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 15, .ParentID = 4, .Name = "Howard M. Carpenter", .Department = "Production", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 16, .ParentID = 4, .Name = "Jennifer T. Tapia", .Department = "Production", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 17, .ParentID = 5, .Name = "Judith P. Underhill", .Department = "Finance", .Position = "Manager"})
stuff.Add(New Employee() With {.ID = 18, .ParentID = 5, .Name = "Russell E. Belton", .Department = "Finance", .Position = "Manager"})
Return stuff
End Function
End Class
End Namespace
using System.Windows;
using DevExpress.Xpf.Grid;
namespace DXTreeList_NodeTraversing {
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window {
public MainWindow() {
InitializeComponent();
treeListControl1.ItemsSource = Stuff.GetStuff();
}
private void treeListControl1_Loaded(object sender, RoutedEventArgs e) {
SmartExpandNodes(4);
}
private void SmartExpandNodes(int minChildCount) {
TreeListNodeIterator nodeIterator = new TreeListNodeIterator(treeListView1.Nodes, true);
while (nodeIterator.MoveNext())
nodeIterator.Current.IsExpanded = nodeIterator.Current.Nodes.Count >= minChildCount;
}
}
}
using System;
using System.Collections.Generic;
namespace DXTreeList_NodeTraversing {
public class Employee {
public int ID { get; set; }
public int ParentID { get; set; }
public string Name { get; set; }
public string Position { get; set; }
public string Department { get; set; }
}
public static class Stuff {
public static List<Employee> GetStuff() {
List<Employee> stuff = new List<Employee>();
stuff.Add(new Employee() { ID = 1, ParentID = 0, Name = "Gregory S. Price", Department = "", Position = "President" });
stuff.Add(new Employee() { ID = 2, ParentID = 1, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" });
stuff.Add(new Employee() { ID = 3, ParentID = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President" });
stuff.Add(new Employee() { ID = 4, ParentID = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" });
stuff.Add(new Employee() { ID = 5, ParentID = 1, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" });
stuff.Add(new Employee() { ID = 6, ParentID = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 7, ParentID = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 8, ParentID = 2, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 9, ParentID = 2, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" });
stuff.Add(new Employee() { ID = 10, ParentID = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 11, ParentID = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 12, ParentID = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 13, ParentID = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" });
stuff.Add(new Employee() { ID = 14, ParentID = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 15, ParentID = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 16, ParentID = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" });
stuff.Add(new Employee() { ID = 17, ParentID = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" });
stuff.Add(new Employee() { ID = 18, ParentID = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" });
return stuff;
}
}
}