Event-driven Property Change Notification Made Easy

Sometimes a CLR object provides a property for reading value and an event for notifying of value changing/changed. For these objects, we could listen for the event and provide property change notification to WPF data binding engine.

The Wrapper way of doing this

Say we have an instance of SomeType which exposes a SomeProperty along with a SomePropertyChanged event fires when SomeProperty's value is changed. We could manually write a wrapper class for the type to route event-based change notification to INotifyPropertyChanged mechanism which WPF data binding engine awares.

class SomeObjectWrapper: INotifyPropertyChanged
{
  private SomeType _instance;

  public ActiveObjectWrapper(SomeType instance)
  {
    _instance = instance;
    _instance.SomePropertyChanged += delegate
    {
      PropertyChanged(_instance, new PropertyChangedEventArgs("SomeProperty"))
    };
  }

  public string SomeProperty { get { return _instance.SomeProperty; } }
}

The Active Data Object of doing the same...

MakeAlive<DataObject>("SomeProperty", "SomePropertyChanged");

Last edited May 20, 2008 at 10:23 AM by JGTM, version 1

Comments

No comments yet.