PRISM 5 potential BindableBase Class Update

Jul 26, 2014 at 4:01 AM
Edited Jul 26, 2014 at 4:18 AM

I really like the bindablebase class in PRISM 5, however it looked like it was lacking a little bit in its 'SetProperty' method. I personally am not a huge fan of using the static string name for the OnPropertyChanged method call as I find it to be bit error prone and hard to debug, so I tend to use the expression implementation of OnPropertyChanged. As such, I assumed there would be a 'SetProperty' method that would work in much the same way
but much to my chagrin, there is not anyway to call the 'SetProperty' method using an expression.

So here is another implementation of the SetProperty method that has an optional expression instead of string that calls the notify property changed event. Here's the code I used in my code base to accomplish it, maybe it will help others:
protected bool SetProperty<T>(ref T storage, T value, Expression<Func<T>> propertyExpression)
       if (base.SetProperty<T>(ref storage, value))
           var _propertyName = Microsoft.Practices.Prism.Mvvm.PropertySupport.ExtractPropertyName(propertyExpression);
           return true;
       else return false;