Skip to main content
A newer version of this page is available.

Implement Dependent Reference Properties (XPO)

  • 4 minutes to read

In this lesson, you will learn how to implement properties whose values can depend on other properties. The Manager property will be added to the Contact class. By default, it will be represented by a lookup editor containing all Contacts that exist in the database. However, you may need this editor to contain Contacts from the same Department. In addition, you may need the Position of these Contacts to be “Manager”. To do this, use the DataSourcePropertyAttribute and DataSourceCriteriaAttribute attributes for the Manager property.

  • Add a new Manager property of the Contact type to the Contact class. Apply the DataSourceProperty attribute to this property, as shown below.

    [DefaultClassOptions]
    public class Contact : Person {
        //...
        private Contact manager;
        [DataSourceProperty("Department.Contacts")]   
        public Contact Manager {
           get { return manager; }
           set { SetPropertyValue(nameof(Manager), ref manager, value); }
        }
        //...
    }
    

    With the DataSourceProperty attribute applied, the Manager lookup editor will contain Contact objects that are specified by the Department object’s Contacts property.

  • Run the application and select Contact in the drop-down list of the New combo box. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Make sure that the Department property of the listed objects is the same as those you specified above.

    Tutorial_BMD_Lesson7_1

  • Apply the DataSourceCriteria attribute to the Contact class’ Manager property as shown below.

    public class Contact : Person {
        //...
        [DataSourceProperty("Department.Contacts")]
        [DataSourceCriteria("Position.Title = 'Manager' AND Oid != '@This.Oid'")]
        public Contact Manager {
            // ...
        }
        // ...
    }
    

    With the DataSourceCriteria attribute applied, the Manager lookup editor will contain Contact objects that satisfy the criteria specified in the attribute parameter.

  • Run the application. Set the Position property to “Manager” for several Contact objects.

    Managers_XAF

  • Select Contact in the New (new_dropdown_btn) button’s drop-down list. The Contact Detail View will be invoked. Specify the Department property and expand the Manager lookup editor. Check to make sure that the Position property is set to “Manager” for each of the listed objects.

    Tutorial_BMD_Lesson7_2

  • If the Department property is not specified for a Contact, you can provide another data source for the Manager lookup editor. To do this, specify the second parameter for the DataSourceProperty attribute. In the code below, this parameter is set to the DataSourcePropertyIsNullMode.SelectAll value. You can also set the DataSourcePropertyIsNullMode.SelectNothing or DataSourcePropertyIsNullMode.CustomCriteria values. In the latter case, a third parameter is required to specify a criterion.

    [DefaultClassOptions]
    public class Contact : Person {
        //...
        [DataSourceProperty("Department.Contacts",DataSourcePropertyIsNullMode.SelectAll)]
        [DataSourceCriteria("Position.Title = 'Manager' AND Oid != '@This.Oid'")]
        public Contact Manager {
            // ...
        }
        // ...
    }
    

    The code above will show all contacts in the Manager lookup editor, if the Department property is not specified.

  • Run the application and check the results.

Note

You can implement the same behavior at design time. For details, refer to the Filter Lookup Editor Data Source lesson.

You can see the code demonstrated here in the MySolution.Module | Business Objects | Contact.cs (Contact.vb) file of the Main Demo installed with XAF. The MainDemo application is installed in %PUBLIC%\Documents\DevExpress Demos 20.2\Components.NET Core Desktop Libraries\eXpressApp Framework\MainDemo by default. The ASP.NET Web Forms version is available online at https://demos.devexpress.com/XAF/MainDemo.

 

Next Lesson: Implement Property Value Validation in Code (XPO)

See Also