Implement Dependent Reference Properties (XPO)
- 3 minutes to read
This lesson explains how to implement properties whose values can depend on other properties.
You will add a new Manager property to the Contact class. The editor for this property will display a list of managers who work in the same department.
Note
Before you proceed, take a moment to review the following lessons:
Step-by-Step Instructions
Add a new Manager property of the Contact type to the Contact class.
[DefaultClassOptions] public class Contact : Person { //... private Contact manager; public Contact Manager { get { return manager; } set { SetPropertyValue(nameof(Manager), ref manager, value); } } //... }
Apply the DataSourceProperty and DataSourceCriteria attributes to the newly added property.
[DefaultClassOptions] public class Contact : Person { //... [DataSourceProperty("Department.Contacts", DataSourcePropertyIsNullMode.SelectAll)] [DataSourceCriteria("Position.Title = 'Manager' AND Oid != '@This.Oid'")] public Contact Manager { // ... } // ... }
Run the application.
Add the following objects:
- a Department object (for example, “Developer Department”)
- multiple Contact objects with the Department property set to “Developer Department”
- multiple Position objects (for example, “Manager”, “Developer”).
For the newly added contacts, set the Position property to:
- “Manager” (for multiple Contact objects);
- “Developer” (for other Contact objects).
Create a new Contact object. In the Contact Detail View, specify the Department property and expand the Manager lookup editor. Notice the following:
- The Department property of the listed objects is the same as those you specified above.
- The Position property is set to “Manager” for each of the listed objects.
Detailed Explanation
You can use integrated XAF designers to implement the same behavior without code. For details, refer to the following lesson: Filter Lookup Editor Data Source.
The DataSourceProperty Attribute
The DataSourceProperty attribute accepts two parameters. The first one specifies the path to the lookup list. The second 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, the second parameter is set to SelectAll. You can also set the SelectNothing or CustomCriteria values. In the latter case, a third parameter is required to specify criteria.
The DataSourceCriteria Attribute
With the DataSourceCriteria attribute applied, the Manager lookup editor contains Contact objects that satisfy the criteria specified in the attribute parameter.
Next Lesson
Implement Property Value Validation in Code (XPO)