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

PropertyChanged Event in Business Classes

  • 3 minutes to read

To provide a consistent user interface, your XAF application must receive notifications from business classes when their property values are changed. For instance, Conditional Appearance and Validation modules may require an immediate UI update when a user modifies a certain value. This topic describes how to provide notifications from XPO, Entity Framework and non-persistent business classes.

The notification mechanism is based on supporting the standard INotifyPropertyChanged interface and implementing its declared PropertyChanged event. To send a notification to internal XAF code, trigger PropertyChanged from a property set accessor within a business class.

PropertyChanged Event in XPO

There is no need to implement the INotifyPropertyChanged interface manually in an XPO business class. When you declare an XPO business class, you inherit BaseObject (or another base persistent class) that already supports this interface. You can trigger the PropertyChanged event from a property set accessor by executing the XPBaseObject.OnChanged or PersistentBase.SetPropertyValue helper method.

public class Department : BaseObject {
    // ...
    private string title;
    public string Title {
        get {
            return title;
        }
        set {
            SetPropertyValue(nameof(Title), ref title, value);
        }
    }
}

See Also: Simplified Property Syntax

PropertyChanged Event in Entity Framework

When implementing an EntityFramework business class, you do not inherit any standard base class. Thus, you should implement the INotifyPropertyChanged interface manually. XAF provides the XAF Business Object | EF Business Object project item template to simplify this task.

EFBusinessOblect

In an existing class, you can implement INotifyPropertyChanged as follows.

public class Customer : INotifyPropertyChanged {
    // ...
    public event PropertyChangedEventHandler PropertyChanged;
    protected void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
}

You can use the OnPropertyChanged helper method to trigger the PropertyChanged event from a property set accessor.

public class Customer : INotifyPropertyChanged {
    // ...
    private CustomerStatus status;
        public CustomerStatus Status {
        get { return status; }
        set {
            if(status != value) {
                status = value;
                OnPropertyChanged();
            }
        }
    }
}

PropertyChanged Event in Non-Persistent Classes

To use the PropertyChanged event in a non-persistent class, follow the same approach which is described above for the Entity Framework.

public class NonPersistentObject1 : INotifyPropertyChanged {
    // ...
    private string sampleProperty; 
    public string SampleProperty {
        get { return sampleProperty; }
        set {
            if (sampleProperty != value) {
                sampleProperty = value;
                OnPropertyChanged();
            }
        }
    }
    private void OnPropertyChanged([System.Runtime.CompilerServices.CallerMemberName] string propertyName = null) {
        PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
    }
    public event PropertyChangedEventHandler PropertyChanged;
}

To create a new non-persistent class that supports INotifyPropertyChanged, use the XAF Business Object | Non-Persistent Object project item template.

NonPersistentObject