While I was testing something with Prism4 + MEF, I found this interesting method overload to raise PropertyChanged events without having to use the string name of the property itself.
Because having a typo in such a string can be very painful.
Let’s say you have a ViewModel which derives from the NotificationObject. In the setter I’m using the ‘better’ way of raising that PropertyChanged event:
public class TestViewModel : NotificationObject
{
private string _Title;
public string Title
{
get { return _Title; }
set
{
if (value != _Title;)
{
_Title = value;
this.RaisePropertyChanged(() => this.Name);
}
}
}
}
instead of writing:
this.RaisePropertyChanged("Title");
If you have already your project full of RaisePropertyChanged methods with strings you can easily replace them in Visual Studio with Regular Expressions:
- [Ctrl]+[F]
- Select [Quick Replace]
- In the dropdown menu choose [Replace in Files] instead of [Quick Replace]
- Look in: [Entire Solution]
- check [Use] and select [Regular expressions]
- Find what: RaisePropertyChanged\(\”{[:a]*}\”\)\;
- Replace with: RaisePropertyChanged(() => this.\1);
Have fun
