Skip to main content
All docs
V24.1

TreeViewControl.KeyFieldName Property

Gets or sets the name of the service field in a data source that contains unique values. This is a dependency property.

Namespace: DevExpress.Xpf.Grid

Assembly: DevExpress.Xpf.Grid.v24.1.dll

NuGet Package: DevExpress.Wpf.Grid.Core

Declaration

public string KeyFieldName { get; set; }

Property Value

Type Description
String

The name of the data source key field.

Remarks

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

To build a tree structure, your data source should contain the following two fields with the same data type:

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

In this mode, you can specify the RootValue property to add to the collection of root nodes only nodes with the specified parent field value.

Note

Fields that specify the KeyFieldName and ParentFieldName properties must be of the same type. These values (key and parent) cannot be equal for a node.

<dxg:TreeViewControl ItemsSource="{Binding Employees}" 
                     TreeDerivationMode="Selfreference"
                     KeyFieldName="ID" 
                     ParentFieldName="ParentID" 
                     TreeViewFieldName="Name">
    <dxg:TreeViewControl.RootValue>
        <sys:Int32>1</sys:Int32>
    </dxg:TreeViewControl.RootValue>
</dxg:TreeViewControl>
using System.Windows;
using System.Collections.Generic;
using DevExpress.Mvvm;

namespace TreeViewSelfreference {
    public class Employee : BindableBase {
        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 Staff {
        public static List<Employee> GetStaff() {
            List<Employee> staff = new List<Employee> {
                new Employee() { ID = 1, ParentID = 0, Name = "Gregory S. Price", Department = "", Position = "President" },
                new Employee() { ID = 2, ParentID = 1, Name = "Irma R. Marshall", Department = "Marketing", Position = "Vice President" },
                new Employee() { ID = 3, ParentID = 1, Name = "John C. Powell", Department = "Operations", Position = "Vice President" },
                new Employee() { ID = 4, ParentID = 1, Name = "Christian P. Laclair", Department = "Production", Position = "Vice President" },
                new Employee() { ID = 5, ParentID = 1, Name = "Karen J. Kelly", Department = "Finance", Position = "Vice President" },
                new Employee() { ID = 6, ParentID = 2, Name = "Brian C. Cowling", Department = "Marketing", Position = "Manager" },
                new Employee() { ID = 7, ParentID = 2, Name = "Thomas C. Dawson", Department = "Marketing", Position = "Manager" },
                new Employee() { ID = 8, ParentID = 2, Name = "Angel M. Wilson", Department = "Marketing", Position = "Manager" },
                new Employee() { ID = 9, ParentID = 2, Name = "Bryan R. Henderson", Department = "Marketing", Position = "Manager" },
                new Employee() { ID = 10, ParentID = 3, Name = "Harold S. Brandes", Department = "Operations", Position = "Manager" },
                new Employee() { ID = 11, ParentID = 3, Name = "Michael S. Blevins", Department = "Operations", Position = "Manager" },
                new Employee() { ID = 12, ParentID = 3, Name = "Jan K. Sisk", Department = "Operations", Position = "Manager" },
                new Employee() { ID = 13, ParentID = 3, Name = "Sidney L. Holder", Department = "Operations", Position = "Manager" },
                new Employee() { ID = 14, ParentID = 4, Name = "James L. Kelsey", Department = "Production", Position = "Manager" },
                new Employee() { ID = 15, ParentID = 4, Name = "Howard M. Carpenter", Department = "Production", Position = "Manager" },
                new Employee() { ID = 16, ParentID = 4, Name = "Jennifer T. Tapia", Department = "Production", Position = "Manager" },
                new Employee() { ID = 17, ParentID = 5, Name = "Judith P. Underhill", Department = "Finance", Position = "Manager" },
                new Employee() { ID = 18, ParentID = 5, Name = "Russell E. Belton", Department = "Finance", Position = "Manager" }
            };
            return staff;
        }
    }
    public class MainWindowViewModel : ViewModelBase {
        public MainWindowViewModel() {
            Employees = Staff.GetStaff();
        }
        public List<Employee> Employees { get; private set; }
    }
}
See Also