Skip to main content
A newer version of this page is available. .
.NET Framework 4.5.2+

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.v20.2.dll

NuGet Package: DevExpress.ExpressApp

Declaration

event EventHandler<ObjectChangedEventArgs> ObjectChanged

Event Data

The ObjectChanged event's data class is ObjectChangedEventArgs. 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;
    }
}

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.

The following code snippets (auto-collected from DevExpress Examples) contain references to the ObjectChanged event.

Note

The algorithm used to collect these code examples remains a work in progress. Accordingly, the links and snippets below may produce inaccurate results. If you encounter an issue with code examples below, please use the feedback form on this page to report the issue.

See Also