Skip to content
Marek Fišera edited this page Nov 18, 2015 · 2 revisions

Observables provides implementations of INotifyPropertyChanged, INotifyCollectionChanged and other WPF based contracts.

For defining observable objects, those that implement INotifyPropertyChanged, simply extend from ObservableObject. There is defined protected method RaisePropertyChanged for raising property changed event. For simplicity, this methods uses CallerMemberNameAttribute to implicitly get caller property name to raise event for. If needed, implicit value can be replaced by use defined.

Standard ObservableCollection from System.dll is extended by AddRange method and by raising Count property changed event on adding items. Beside this collection, there is also ObservableDictionary and SortableObservableCollection.

PropertyChangedObserver

PropertyChangedObserver is one way of simplifying property changed handlers. This class is created with object instance to listen on and has methods for subscribing and unsubscribing handlers on single property change.

MainModel model = new MainModel();
PropertyChangedObserver<MainModel> modelObserver = new PropertyChangedObserver<MainModel>(model);

modelObserver.Add(m => m.FirstName, m => firstNameCounter++);
modelObserver.Add(m => m.LastName, m => lastNameCounter++);
modelObserver.Add(m => m.FullName, m => fullNameCounter++);

Beside LINQ expression methods for adding and removing handlers, those with simple string parameter are provided too.

Dispose of observer instance causes unsubscription from PropertyChanged on passed instance and possible release of it by Garbage collector.

Clone this wiki locally