Today, working on a new feature for my pet project, I realized that I have to notify the view, that all properties in view model have changed. The most obvious way to achieve that would, of course be to rise PropertyChange event a bunch of times.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
protected virtual void OnPropertyChanged(Expression<Func<object>> propertyExpression) { //.. //implementation //.. } protected virtual void OnPropertyChanged(string propertyName) { //.. //implementation //.. } public void Refresh() { OnPropertyChanged(() => FirstProperty); OnPropertyChanged(() => SecondProperty); OnPropertyChanged(() => ThirdProperty); //and so on ... } |
This is good solution for one time usage, however I was interested in something more general, something which could be extracted to base view model. Fortunately, it turns out that there is a simple trick to do that. All You have to do is use an empty string or null as a property name. So in my case this comes down to this one-liner
1 2 3 4 |
public void Refresh() { OnPropertyChanged(string.Empty); } |
It's great. I was trying to achieve that just a couple of days ago. Thanks for the tip 🙂