This example demostrates how to implement self-referenced persistent objects and display them within the XtraTreeList. A simple example of a self-referenced object is an Employee. An employee reports to a Manager, but at the same time, this employee might also be a manager for other employers, referred to as Subordinates.
A self-referenced object has a parent object property and a child collection. Here is the persistent Employee class. It has a Manager property of type Employee and Subordinates of type XPCollection<Employee>
. Both properties are decorated with the Association
attribute. As such, the Employee type appears on both sides of a one-to-many association.
C#
using DevExpress.Xpo;
public class Employee : XPObject {
public Employee(Session session) : base(session) { }
string _FullName;
public string FullName {
get { return _FullName; }
set { SetPropertyValue("FullName", ref _FullName, value); }
}
private string _Title;
public string Title {
get { return _Title; }
set { SetPropertyValue("Title", ref _Title, value); }
}
Employee _Manager;
[Association("ManagerSubordinates")]
public Employee Manager {
get { return _Manager; }
set { SetPropertyValue("Manager", ref _Manager, value); }
}
[Association("ManagerSubordinates")]
public XPCollection<Employee> Subordinates {
get { return GetCollection<Employee>("Subordinates"); }
}
}
VB
Imports DevExpress.Xpo
Public Class Employee
Inherits XPObject
Public Sub New(ByVal session As Session)
MyBase.New(session)
End Sub
Private _FullName As String
Public Property FullName() As String
Get
Return _FullName
End Get
Set(ByVal value As String)
SetPropertyValue("FullName", _FullName, value)
End Set
End Property
Private _Title As String
Public Property Title() As String
Get
Return _Title
End Get
Set(ByVal value As String)
SetPropertyValue("Title", _Title, value)
End Set
End Property
Private _Manager As Employee
<Association("ManagerSubordinates")> _
Public Property Manager() As Employee
Get
Return _Manager
End Get
Set(ByVal value As Employee)
SetPropertyValue("Manager", _Manager, value)
End Set
End Property
<Association("ManagerSubordinates")> _
Public ReadOnly Property Subordinates() As XPCollection(Of Employee)
Get
Return GetCollection(Of Employee)("Subordinates")
End Get
End Property
End Class
A collection of self-referenced objects can be displayed in the XtraTreeList. TreeList properties must be set as shown below.
C#
treeList1.KeyFieldName = "This";
treeList1.ParentFieldName = "Manager!";
treeList1.RootValue = null;
treeList1.DataSource = xpCollection1;
VB
treeList1.KeyFieldName = "This"
treeList1.ParentFieldName = "Manager!" treeList1.RootValue = Nothing
treeList1.DataSource = xpCollection1
- Employee.cs (VB: Employee.vb)
- Form1.cs (VB: Form1.vb)
(you will be redirected to DevExpress.com to submit your response)