views:

46

answers:

2

Hi,

In MVVM pattern, how to notify all properties of the view model has changed? I don' t want to call all notifypropertychanged event of all properties.

I have an entity class and in view model I wrote all of the public fields of the entity as public properties. I want to rebind new entity and just write a single line of code to notify that all properties has changed?

Thanks for your help.

+3  A: 

Just raise the PropertyChanged event with an empty string as the property name :

OnPropertyChanged(String.Empty);
Thomas Levesque
Yes, firing the PropertyChanged event with null or an empty string does the trick.
Mark Green
A: 

Ok what I understood from your question is this..

View <> ViewModel <> Entity (with a bunch of properties)

View is bound to ViewModel, which delegates to Entity. You now want to replace the backing entity and signal the view to refresh to update to the newer entity.

I'd suggest implementing an indexer in the ViewModel which takes in a string (the name of the backing property). The ViewModel can be used as a cache if required, delegating to the backing entity only on a miss.

When you replace the entity, the viewModel clears its cache and fires NotifyPropertyChanged (in the indexer set) with String.Empty or null. Which I learned today, indicates all properties have changed.

This way you don't create n delegating properties in the ViewModel. A google search result shows that it has been tried at least once with success.

Gishu