The Devexpress TreeList control is bound to display the parent-child node image and devexpresstreelist.
Today, a colleague consulted the Devexpress TreeList control to automatically display the parent-child node image. However, the result does not show the parent-child node relationship, but shows all nodes as parent nodes, the code for the image class is as follows:
public class Item:XPBaseObject
{
public Item()
: base()
{
}
public Item(Session session)
: base(session)
{
}
[Key(true)]
public int Id{ get;set; }
public string Name{get;set;}
[Association("SubItems"),Persistent("ParentId")]
public Item ParentItem;
[Association("SubItems", typeof(Item)), Aggregated]
public XPCollection<Item> SubItems
{
get { return GetCollection<Item>("SubItems"); }
}
}
1 // ... omitted
2
3 this.treeList1.KeyFieldName = "Id";
4 this.treeList1.Name = "treeList1";
5 this.treeList1.ParentFieldName = "ParentId";
6
7 // ... omitted
The actual data structure automatically generated by XPO in the database also meets our design requirements. The entered data is correct, but the result is not displayed based on the parent-child node relationship. Finally, find the difference in the previous code, that is, this. treeList1.ParentFieldName = "ParentId"; the field name is not declared in the object class, although the field name ParentId is specified in the property ParentItem, it is only used to store the foreign key relationship to the field ParentId. The TreeList control does not intelligently read this value as the primary key value of the parent node. You must declare a read-only ParentId attribute in the object class to solve this problem. See the following code:
1 public class Item: XPBaseObject
2 {
3
4 // ... omitted
5
6
7 [NonPersistent]
8 public int ParentId
9 {
10 get {return ParentItem == null? 0: this.ParentItem.Id;}
11}
12
13
14 // ... omitted
15
16
17}
Summary: The fields bound to the TreeList control can be bound only when the attributes of the object class exist, if the attribute does not exist, you can write a read-only file that does not need to be stored in the database for private binding.