1
Vote

PropertyObserver: added handlers that pass property name & CollectionObserver

description

Since the PropertyChangedEventArgs already has the property name, and it is frequently (at least for me!) the case that I'm just passing the change notification from a model property to a ViewModel property of the same name. I added a second PropertyObserver.Register method that takes a two parameter action (the second parameter is string). When invoked the property name is passed as the value of that parameter.
 
Also, I added a CollectionObserver class that does WeakEvent connection to INotifyCollectionChanged. It catches changes to the collection itself (unlike the Charles Petzold msdn article, which deals with changes to the items IN the collection.) The registrations are specific by the enum NotifyCollectionChangedAction.
 
Code (and simple unit tests) attached.

file attachments

comments

JoshSmithOnWpf wrote Aug 23, 2009 at 6:56 PM

Thanks for this suggestion, Matt. At this time I'm not going to add it to the library, because I have never found myself wanting this particular feature, and it introduces a new type to the library (CollectionObserver). I might be overly conservative, but my overarching goal with MVVM Foundation is to keep it as minimal as possible. Perhaps one day I will come around, and add it. Thanks again!

wrote Aug 23, 2009 at 6:56 PM

JoshSmithOnWpf wrote Aug 23, 2009 at 7:08 PM

feature declined



** Closed by JoshSmithOnWpf 8/23/2009 11:56 AM

JoshSmithOnWpf wrote Aug 23, 2009 at 7:08 PM

After revisiting this item, I realized that you are proposing two new, unrelated additions. I should have been more thorough before closing this item! I very much like the CollectionObserver class, and can think of several situations in which I would have used it. I will reconsider this proposal. Great work!

MattHeffron wrote Aug 24, 2009 at 5:34 PM

The justification for the two parameter handler in the PropertyObserver.RegisterHandler (not .Register, a typo in the original Description above) is where the View Model has a property which has the same name as a Model property but is just a controlled formattingt of the value.
E.g.,
public string RealTemperature {  get { return GetFormattedOutputTemperature(TheRunConditions.RealTemperature); }  }
The handler registered on the RealTemperature property of the model (TheRunConditions) needs only to raise the PropertyChanged event for the ViewModel in order for WPF to update the display. Since there are several such property-pairs in my Model-ViewModel, I have a simple handler that is registered for them all:
RunConditionsObserver = new PropertyObserver<RunConditions>(TheRunConditions)
                          .RegisterHandler(rc => rc.RealTemperature, PassThroughRaisePropertyChanged)
                          .RegisterHandler( ... etc

void PassThroughRaisePropertyChanged(object ignore, string propertyName)
{ RaisePropertyChanged(propertyName); }
In this way the registered handlers don't need to have the property names "hard coded".

wrote Feb 21, 2013 at 11:05 PM