Display a Tree List using the HCategory Class
- 3 minutes to read
To display data in a tree-like structure, the ITreeNode interface should be implemented in the appropriate business classes. The objects that support this interface are displayed via the ASPxTreeList and TreeList controls from the ASPxTreeList and XtraTreeList libraries. This is provided by the TreeList Editors module. To learn more about the ITreeNode interface and the List Editor supplied for it with the TreeList Editors module, refer to the TreeList Editors Module Overview topic. This topic demonstrates how to use the ready-to-use HCategory class that implements the ITreeNode interface. This class is supplied with the Business Class Library, so you can easily use it instead of implementing the ITreeNode class from scratch.
Tip
A complete sample project is available in the DevExpress Code Examples database at https://supportcenter.devexpress.com/ticket/details/e1125/xaf-winforms-how-to-use-tree-list-editors-to-display-list-views.
The HCategory class is implemented so that its Parent and Children properties can reference objects of the HCategory type only.
To add the Business Class Library‘s HCategory class to your business model, use the Designer of the required module (application module or Windows Forms module). First, check that the DevExpress.Persistent.BaseImpl.Xpo.v24.1.dll assembly is referenced in this module. Then, invoke the Module Designer. In the Exported Types section, select the Referenced Assemblies | DevExpress.Persistent.BaseImpl | HCategory item. Mark it bold by pressing the SPACEBAR. Do not forget to rebuild the solution after making changed in a Designer.
To add the HCategory class to the navigation control, invoke the Model Editor for the current module. Add the HCategory node to the NavigationItems node (see Add an Item to the Navigation Control).
To display the HCategory object as a tree, add the TreeList Editors module to the application. Since this module supplies Windows Forms specific instruments only, add it to the Windows Forms application project. For this purpose, invoke the Application Designer and drag the module from the Toolbox to the Modules section.
Run the Windows Forms application. Click the HCategory item in the navigation control. Then, create HCategory objects via the New Action. You will see that these objects will be displayed as a tree by the HCategory List View:
You can also display a collection of items for each HCategory node to the right of the tree list. For details, refer to the Categorized List topic.
If you need to extend the HCategory class, inherit from it.
If you need to implement the ITreeNode interface from scratch, refer to the Display a Tree List using the ITreeNode Interface topic for details.
To see the HCategory class implementation, refer to the %PROGRAMFILES%\DevExpress 24.1\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.Xpo (or %PROGRAMFILES%\DevExpress 24.1\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.EFCore) folder, where you can find the HCategory.cs file.
Note
The TreeListEditor and CategorizedListEditor cannot properly display a tree if the List View’s object type is a descendant of the base type, which implements the ITreeNode interface, and the hierarchy contains base type objects.
For example, suppose you have a tree formed by HCategory descendant objects, which have child HCategory objects. If you try to invoke a List View for an HCategory descendant type, the TreeListEditor and CategorizedListEditor will not be able to correctly display this structure. To correctly display such a tree, invoke a List View for the base type, in this instance - HCategory.