RaisePropertyChanging and RaisePropertyChanged automatically

Dec 27, 2012 at 6:10 PM

We have added a method to a new base class that inherits from ViewModelBase. The only method in this class.

protected void SetProperty<T>(Expression<Func<T>> propertyExpression, T newValue)
			string name = this.GetPropertyName<T>(propertyExpression);
			Type myType = this.GetType();
			PropertyInfo prop = myType.GetProperty(name);
			prop.SetValue(this, newValue);

Is this something that can be added to ViewModelBase? It greatly simplifies the process for us so we don't have to provide setters and getters for all the properties.

Dec 27, 2012 at 6:35 PM


I am not sure I see the value over ViewModelBase.Set which is already available. Can you explain?



Dec 27, 2012 at 7:03 PM
Edited Dec 27, 2012 at 7:25 PM

I hadn't seen the Set<T> method honestly. We are just getting going with MvvmLight. However, when I switch to using the Set<T> method instead of the method I posted earlier I don't get my UI updating.

A couple of points which may or may not be reasons to add an additional overload to Set or modify the current implementation of the Set method.

  • The method I posted calls GetPropertyName on ObservableObject only once whereas the existing implementation of Set<T> will call it twice since it is using the strongly typed version of RaisePropertyChanging and RaisePropertyChanged.
  • Using the method I posted doesn't require the use of the field parameter as used in Set<T> so if the user wants to use automatic properties they can.

Maybe I'm not using Set<T> correctly and that is the problem. Do you have an example usage?

*EDIT: When I use Set<T> as suggested I see that the backing field gets updated as expected but my UI never gets updated. Not sure what other information you would need. No special messenger stuff going on in our application.