Implement Dependent Reference Properties
- 3 minutes to read
This lesson explains how to implement properties whose values depend on other properties.
For this purpose, add a new Manager
property to the Employee
class. The editor for this property displays a list of managers who work in the same department.
Note
Before you proceed, take a moment to review the previous lessons:
Step-by-Step Instructions
Add the new
Manager
property to theEmployee
class:using DevExpress.ExpressApp.DC; //... public class Employee : BaseObject { //... public virtual Employee Manager { get; set; } }
Apply the
DataSourceProperty
andDataSourceCriteria
attributes to the newly added property:using DevExpress.ExpressApp.DC; //... public class Employee : BaseObject { //... [DataSourceProperty("Department.Employees", DataSourcePropertyIsNullMode.SelectAll), DataSourceCriteria("Position.Title = 'Manager'")] public virtual Employee Manager { get; set; } } // ...
In this code, the
DataSourceProperty
attribute accepts two parameters:dataSourceProperty
andmode
.The
dataSourceProperty
parameter is a string value that specifies the name of the collection property used as the data source for a List View displayed in a Lookup Property Editor. In this tutorial, you set this parameter to"Department.Employees"
.The
mode
parameter is optional. It specifies how the lookup items are populated if the application could not find any items from the path. In this tutorial, you set it to SelectAll.You can also set it to SelectNothing or CustomCriteria. In the latter case, you also have to use the
DataSourcePropertyIsNullCriteria
parameter to specify criteria to filter the List View by the target property’s Lookup Property Editor.The
DataSourceCriteria
attribute restricts the Manager lookup editor items to specific objects. With the"Position.Title = 'Manager'"
filter, the lookup editor displays only theEmployee
objects whosePosition
property value is"Manager"
.Add a migration and update the database. See the following section for details: Use a DBMS: Setup Migrations.
Run the application and make the following changes:
- Add a Department object (for example, “Development”).
- Add multiple Position objects (for example, “Manager”, “Developer”, “QA”).
- Add multiple Employee objects with the Department property set to “Development”.
- Set the Position property of two Employee objects to “Manager”.
- Set the Position property of the remaining Employee objects to “Developer”.
Create a new
Employee
object. In the Employee Detail View, specify theDepartment
property and expand the Manager lookup editor. Notice that it only shows the managers from the specified department.- ASP.NET Core Blazor
- Windows Forms
Tip
You can use integrated XAF designers to implement the same behavior without code. For details, refer to the following article: How to: Implement Cascading Filtering for Lookup List Views.