The requested page is not available for the requested platform. You are viewing the content for Default platform.

IObjectSpace.ObjectChanged Event

Occurs when a persistent object is created, deleted or changed (when the objects' INotifyPropertyChanged.PropertyChanged event occurs).

Namespace: DevExpress.ExpressApp

Assembly: DevExpress.ExpressApp.v19.1.dll

Declaration

event EventHandler<ObjectChangedEventArgs> ObjectChanged
Event ObjectChanged As EventHandler(Of ObjectChangedEventArgs)

Event Data

The ObjectChanged event handler receives an argument of the ObjectChangedEventArgs type. The following properties provide information specific to this event.

Property Description
MemberInfo Specifies an object that is information on the property whose value has been changed.
NewValue Specifies the new value of a changed property.
Object Provides access to the object that is being manipulated.
(Inherited from ObjectManipulatingEventArgs)
OldValue Specifies the old value of a changed property.
PropertyName Specifies the name of a property whose value has been changed. Returns null (Nothing in VB) if it is impossible to determine what property causes the change.

Remarks

You can handle this event in a custom Controller to execute business logic when an object is changed. For instance, the following Controller demonstrates how to update the Contact.Office property when the Contact.Department is changed:

using DevExpress.ExpressApp;
// ...
public class ContactBusinessLogicViewController : ObjectViewController<ObjectView, Contact> {
    protected override void OnActivated() {
        base.OnActivated();
        ObjectSpace.ObjectChanged += ObjectSpace_ObjectChanged;
    }
    void ObjectSpace_ObjectChanged(object sender, ObjectChangedEventArgs e) {
        if (View.CurrentObject == e.Object && 
               e.PropertyName == "Department" && 
               ObjectSpace.IsModified && 
               e.OldValue != e.NewValue) {
            Contact changedContact = (Contact)e.Object;
            if (changedContact.Department != null) {
                changedContact.Office = changedContact.Department.Office;
            }
        }
    }
    protected override void OnDeactivated() {
        base.OnDeactivated();
        ObjectSpace.ObjectChanged -= ObjectSpace_ObjectChanged;
    }
}

The ObjectChanged event can be triggered when an end-user starts modifying any property value in a UI. In this instance, the event is triggered, but the ObjectChangedEventArgs.NewValue and ObjectChangedEventArgs.OldValue properties are equal. This is the designed behavior. So, if you want to execute a custom activity when a property value change occurs, check the equality of these properties preliminarily.

Note that the Contact object should support the INotifyPropertyChanged interface and trigger the INotifyPropertyChanged.PropertyChanged event in the Contact.Department setter (see PropertyChanged Event in Business Classes). Otherwise, the ObjectChanged event will not occur. If a Contact is an XPO object, ensure that the SetPropertyValue or OnChanged method is called from the setter (the INotifyPropertyChanged interface is already supported by XPO base classes). If you use Entity Framework, then implement the INotifyPropertyChanged interface manually.

public class Contact : Person, INotifyPropertyChanged {
    private void OnPropertyChanged(String propertyName) {
        if (PropertyChanged != null) {
            PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
        }
    }
    public event PropertyChangedEventHandler PropertyChanged;
    public String Office { get; set; }
    private Department _Department;
    [ImmediatePostData]
    public virtual Department Department {
        get {
            return _Department;
        }
        set {
            _Department = value;
            OnPropertyChanged("Department");
        }
    }
}
Tip

You can use the XAF Business Object | EF Business Object or XAF Business Object | Non-Persistent Object project item template from the Template Gallery to easily add a class that supports INotifyPropertyChanged.

For object changes that cannot be tracked via notification mechanisms exposed by the data layer, the IObjectSpace.SetModified method must be called after an object has been changed. This method adds the object passed as the obj parameter to the list of objects to be committed. To provide custom code after an object has changed, the ObjectChanged event must be raised in the SetModified method.

If you implement the IObjectSpace interface in the BaseObjectSpace class' descendant, you don't have to declare this event. It's declared within the BaseObjectSpace class. The BaseObjectSpace.OnObjectChanged method raises this event. So, you should only invoke the OnObjectChanged method in your BaseObjectSpace.SetModified method override.

Note

The ObjectChangedEventArgs.OldValue and ObjectChangedEventArgs.NewValue parameters return values only for XPO persistent objects and only if corresponding values are passed to the SetPropertyValue or OnChanged method. If you use Entity Framework or non-persistent objects, the OldValue and NewValue parameters always return null (Nothing in VB). This is expected behavior, because Entity Framework doesn't provide an event that the Object Space can handle to get the modified property's old and new values. The INotifyPropertyChanged interface passes the property name only. In this scenario, you can get a new value directly from the property.

See Also