Display a Tree List using the HCategory Class
- 4 minutes to read
To display data in a tree-like structure, implement the ITreeNode interface in the corresponding business classes. XAF uses the DxTreeList
, ASPxTreeList
, and TreeList
controls provided by the TreeList Editors module and DevExpress.ExpressApp.Blazor base module to display objects that support ITreeNode.
For more information about the ITreeNode
interface, refer to the following topic: TreeList Editors Module Overview.
This topic demonstrates the HCategory
class that implements the ITreeNode
interface out of the box. XAF supplies this class with the Business Class Library, and you can 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.
The HCategory
class is implemented in such a way that its Parent
and Children
properties can reference objects of the HCategory
type only.
#Add HCategory Class to Your Business Model
#.NET
In the Solution Explorer, go to the YourSolutionName.Module project and open the Module.cs file. Add the HCategory
class to the AdditionalExportedTypes
collection.
For more information on how to add a business class to your project, refer to the following topic: Ways to Add a Business Class
//...
namespace MySolution.Module;
//...
public sealed class MySolutionModule : ModuleBase {
public MySolutionModule() {
//...
AdditionalExportedTypes.Add(typeof(DevExpress.Persistent.BaseImpl.EF.HCategory));
}
//...
}
Tip
If your application uses Entity Framework Core, register the HCategory
class in your application’s DBContext:
using MySolution.Module.BusinessObjects;
namespace MySolution.Module.BusinessObjects {
public class MySolutionEFCoreDbContext : DbContext {
//...
public DbSet<HCategory> HCategories { get; set; }
}
}
#.NET Framework
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).
- Check that the DevExpress.Persistent.BaseImpl.Xpo.v24.2.dll assembly is referenced in this module.
- Open the Module Designer.
- In the Exported Types section, select the following item: Referenced Assemblies | DevExpress.Persistent.BaseImpl | HCategory. Mark it bold by pressing the space bar.
Do not forget to rebuild the solution after making changes in the Designer.
#Add HCategory to the Navigation Control
To add the HCategory
class to the navigation control, open the Model Editor for the current module and add the HCategory node to the NavigationItems node. For more information, refer to the following topic: Add an Item to the Navigation Control.
#Enable the TreeList Editors Module in Your Application
To display the HCategory
object as a tree, add the TreeList Editors module to the application. For more information, refer to the following section: Add the TreeList Editors Module to Your Application.
#Run the Application
Run the application. Click the HCategory
item in the navigation control. Use the New Action to create HCategory
objects. XAF displays these objects as a tree in the HCategory
List View:
- ASP.NET Core Blazor
- Windows Forms
#Additional Information (Windows Forms)
You can also display a collection of items for each HCategory
node to the right of the tree list. For more information, refer to the following topic: Categorized List (Windows Forms).
To extend the HCategory
class, inherit from it.
To implement the ITreeNode
interface from scratch, refer to the following topic: Display a Tree List using the ITreeNode Interface.
To see the HCategory
class implementation, refer to the following folders where you can find the HCategory.cs file:
- %PROGRAMFILES%\DevExpress 24.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.Xpo
- %PROGRAMFILES%\DevExpress 24.2\Components\Sources\DevExpress.Persistent\DevExpress.Persistent.BaseImpl.EFCore
Note
The Tree
and Categorized
cannot properly display a tree if the List View’s object type derives from the base type that implements the ITree
interface and the hierarchy contains base type objects.
Example: A tree contains HCategory
descendant objects that have child HCategory
objects. When a user invokes a List View for the descendant type (HCategory
), neither Tree
nor Categorized
can correctly display this structure. To correctly display such a tree, you need to invoke a List View for the base type (HCategory
in this case).