Example E3127
Visible to All Users

WPF Tree List - Bind to Self-Referential Data Structure

This example shows how to create a self-referential data structure and display it in the TreeListView.

image

To display this tree structure, your data source should include the following fields of the same data type:

  • Key Field - This field should contain unique values used to identify nodes. Assign its name to the TreeListView.KeyFieldName property.
  • Parent Field - This field should contain values that indicate parent nodes. Assign its name to the TreeListView.ParentFieldName property.

Set the TreeListView.TreeDerivationMode property to Selfreference to enable the self-referential mode.

Files to Review

Documentation

More Examples

Does this example address your development requirements/objectives?

(you will be redirected to DevExpress.com to submit your response)

Example Code

Bind_to_SelfReferential_Data/Employees.cs(vb)
C#
using System.Collections.Generic; namespace Bind_to_SelfReferential_Data { 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 Employees { public static List<Employee> GetStaff() { List<Employee> staff = new List<Employee>(); staff.Add(new Employee() { ID = 1, ParentID = 0, Name = "Gregory S. Price", Department = "", Position = "President" }); staff.Add(new Employee() { ID = 2, ParentID = 1, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" }); staff.Add(new Employee() { ID = 3, ParentID = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President" }); staff.Add(new Employee() { ID = 4, ParentID = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" }); staff.Add(new Employee() { ID = 5, ParentID = 1, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" }); staff.Add(new Employee() { ID = 6, ParentID = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" }); staff.Add(new Employee() { ID = 7, ParentID = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" }); staff.Add(new Employee() { ID = 8, ParentID = 2, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" }); staff.Add(new Employee() { ID = 9, ParentID = 2, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" }); staff.Add(new Employee() { ID = 10, ParentID = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" }); staff.Add(new Employee() { ID = 11, ParentID = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" }); staff.Add(new Employee() { ID = 12, ParentID = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" }); staff.Add(new Employee() { ID = 13, ParentID = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" }); staff.Add(new Employee() { ID = 14, ParentID = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager" }); staff.Add(new Employee() { ID = 15, ParentID = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" }); staff.Add(new Employee() { ID = 16, ParentID = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" }); staff.Add(new Employee() { ID = 17, ParentID = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" }); staff.Add(new Employee() { ID = 18, ParentID = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" }); return staff; } } }
Bind_to_SelfReferential_Data/MainWindow.xaml
XAML
<Window x:Class="Bind_to_SelfReferential_Data.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:Bind_to_SelfReferential_Data" Title="MainWindow" Height="350" Width="525" xmlns:dxg="http://schemas.devexpress.com/winfx/2008/xaml/grid"> <Grid> <dxg:GridControl x:Name="grid"> <dxg:GridControl.Columns> <dxg:GridColumn FieldName="Name" Header="Employee Name"/> <dxg:GridColumn FieldName="Position"/> <dxg:GridColumn FieldName="Department"/> </dxg:GridControl.Columns> <dxg:GridControl.View> <dxg:TreeListView AutoWidth="True" KeyFieldName="ID" ParentFieldName="ParentID" AutoExpandAllNodes="True" TreeDerivationMode="Selfreference"/> </dxg:GridControl.View> </dxg:GridControl> </Grid> </Window>

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confidential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.