Back to Devexpress

TreeViewControl.ChildNodesPath Property

wpf-devexpress-dot-xpf-dot-grid-dot-treeviewcontrol-9ae937ed.md

latest8.0 KB
Original Source

TreeViewControl.ChildNodesPath Property

Gets or sets the name of the field that contains child nodes. This is a dependency property.

Namespace : DevExpress.Xpf.Grid

Assembly : DevExpress.Xpf.Grid.v25.2.dll

NuGet Package : DevExpress.Wpf.Grid.Core

Declaration

csharp
public string ChildNodesPath { get; set; }
vb
Public Property ChildNodesPath As String

Property Value

TypeDescription
String

The name of the field that contains child nodes.

|

Remarks

Use the ChildNodesPath property to bind the TreeViewControl to a collection if all objects have the same field that contains child nodes.

You can use the HasChildNodesPath property to control whether a node has children.

To build a tree structure, set the ChildNodesPath property to a field that contains child nodes (the Employees field in the code sample below).

xaml
<dxg:TreeViewControl ItemsSource="{Binding EmployeeDepartments}" 
                     ChildNodesPath="Employees" 
                     TreeViewFieldName="Name"
                     HasChildNodesPath="HasChildNodes"/>
csharp
using System.Windows;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using DevExpress.Mvvm;

namespace TreeViewChildNodesSelector {
    public class MainWindowViewModel : ViewModelBase {
        public MainWindowViewModel() {
            EmployeeDepartments = Departments.GetDepartments();
        }
        public List<EmployeeDepartment> EmployeeDepartments { get; set; }
    }
    public class Employee {
        public Employee(int id, string name) {
            ID = id;
            Name = name;
        }
        public int ID { get; set; }
        public string Name { get; set; }
    }
    public class EmployeeDepartment {
        public string Name { get; set; }
        public ObservableCollection<Employee> Employees { get; }
        public bool HasChildNodes { get; set; }

        public EmployeeDepartment(string name, IEnumerable<Employee> employees) {
            Name = name;
            Employees = new ObservableCollection<Employee>(employees);
        }
    }
    public static class Departments {
        public static List<EmployeeDepartment> GetDepartments() {
            List<EmployeeDepartment> departments = new List<EmployeeDepartment> {
                new EmployeeDepartment("Management", new Employee[] {
                new Employee(0, "Gregory S. Price")
                }, true),
                new EmployeeDepartment("Marketing", new Employee[] {
                new Employee(1, "Irma R. Marshall"),
                new Employee(2, "Brian C. Cowling"),
                new Employee(3, "Thomas C. Dawson"),
                new Employee(4, "Bryan R. Henderson"),
                }, true),
                new EmployeeDepartment("Operations", new Employee[] {
                new Employee(5, "John C. Powell"),
                new Employee(6, "Harold S. Brandes"),
                new Employee(7, "Jan K. Sisk"),
                new Employee(8, "Sidney L. Holder"),
                }, true),
                new EmployeeDepartment("Production", new Employee[] {
                new Employee(9, "Christian P. Laclair"),
                new Employee(10, "James L. Kelsey"),
                new Employee(11, "Howard M. Carpenter"),
                new Employee(12, "Jennifer T. Tapia"),
                },false),
                new EmployeeDepartment("Finance", new Employee[] {
                new Employee(13, "Karen J. Kelly"),
                new Employee(14, "Judith P. Underhill"),
                new Employee(15, "Russell E. Belton"),
                },false)
            };
            return departments;
        }
    }
}
vb
Imports System.Windows
Imports System.Collections.Generic
Imports System.Collections.ObjectModel
Imports DevExpress.Mvvm

Namespace TreeViewChildNodesSelector
    Public Class MainWindowViewModel
        Inherits ViewModelBase

        Public Sub New()
            EmployeeDepartments = Departments.GetDepartments()
        End Sub

        Public Property EmployeeDepartments As List(Of EmployeeDepartment)
    End Class

    Public Class Employee
        Public Sub New(ByVal id As Integer, ByVal name As String)
            ID = id
            Name = name
        End Sub

        Public Property ID As Integer
        Public Property Name As String
    End Class

    Public Class EmployeeDepartment
        Public Property Name As String
        Public ReadOnly Property Employees As ObservableCollection(Of Employee)
        Public Property HasChildNodes As Boolean

        Public Sub New(ByVal name As String, ByVal employees As IEnumerable(Of Employee))
            Name = name
            Employees = New ObservableCollection(Of Employee)(employees)
        End Sub
    End Class

    Module Departments
        Function GetDepartments() As List(Of EmployeeDepartment)
            Dim departments As List(Of EmployeeDepartment) = New List(Of EmployeeDepartment) From {
                New EmployeeDepartment("Management", New Employee() {New Employee(0, "Gregory S. Price")}, True),
                New EmployeeDepartment("Marketing", New Employee() {New Employee(1, "Irma R. Marshall"), New Employee(2, "Brian C. Cowling"), New Employee(3, "Thomas C. Dawson"), New Employee(4, "Bryan R. Henderson")}, True),
                New EmployeeDepartment("Operations", New Employee() {New Employee(5, "John C. Powell"), New Employee(6, "Harold S. Brandes"), New Employee(7, "Jan K. Sisk"), New Employee(8, "Sidney L. Holder")}, True),
                New EmployeeDepartment("Production", New Employee() {New Employee(9, "Christian P. Laclair"), New Employee(10, "James L. Kelsey"), New Employee(11, "Howard M. Carpenter"), New Employee(12, "Jennifer T. Tapia")}, False),
                New EmployeeDepartment("Finance", New Employee() {New Employee(13, "Karen J. Kelly"), New Employee(14, "Judith P. Underhill"), New Employee(15, "Russell E. Belton")}, False)
            }
            Return departments
        End Function
    End Module
End Namespace

Set the TreeListView.AllowChildNodeSourceUpdates property to true to update child nodes when you set the collection property to a new value.

The following code snippet (auto-collected from DevExpress Examples) contains a reference to the ChildNodesPath property.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

create-an-application-with-the-wpf-treeview-control/CS/TreeViewGettingStarted/MainWindow.xaml#L13

xml
<dxg:TreeViewControl ItemsSource="{Binding EmployeeDepartments}"
                     ChildNodesPath="Employees"
                     TreeViewFieldName="Name"/>

See Also

TreeViewControl Class

TreeViewControl Members

DevExpress.Xpf.Grid Namespace