Refreshing Bindings

Refreshing Bindings

Old forum URL: forums.lhotka.net/forums/t/3276.aspx


David posted on Thursday, July 26, 2007

I have a read only object that can be modified internally, and when this happens I want any bound controls on the form to display the new property values. This is easy to do if the object belongs to a read only collection by calling ResetItem (or OnListChanged), but I can't work out how to do it for a single read only object.

Does anyone know how to do this?

RockfordLhotka replied on Thursday, July 26, 2007

If a property changes, the object should raise a PropertyChanged event as defined by INotifyPropertyChanged.

ReadOnlyBase doesn't implement this interface because read-only objects are read-only, and thus not intended to be changed.

If you have a changing read-only object, then you have two options:

  1. Inherit from BusinessBase so you can call OnPropertyChanged() - remember that you can still implement read-only properties in a BusinessBase subclass, so your object can be effectively read-only, but still has the intrastructure support for changing properties
  2. Implement INotifyPropertyChanged in your business class (making sure to follow the pattern shown in Chapter 3 for BindableBase to avoid serialization issues, and making sure to use the attributes used for the Saved event (as discussed in the 2.1 Handbook), because your event is being declared below UndoableBase in the inheritance hierarchy)

 

David replied on Friday, July 27, 2007

Rocky, thanks for the advice.

Just to clarify why my read-only objects do change, my project has quite a few read-only objects for display or selection purposes. When an editable object is saved I use the channel events (in ActiveObjects) to notify any related objects of the change so they can update themselves internally rather than do a refresh from the database. I find this method works really well and improves perfomance significantly when using a slower network link to the database. So in this sense, read-only means you can't update the object's properties, but it can still change.

Your first suggestion looks like the way to go - quick and simple. Thanks again.

Copyright (c) Marimer LLC