Do folks have any guidance on when a simple .NET property that fires INotifyPropertyChanged.PropertyChanged is sufficient in a view model? Then when do you want to move up to a full blown dependency property? Or are the DPs intended primarily for views?
Do folks have any guidance on when a simple .NET property that fires INotifyPropertyChanged.PropertyChanged
Share
There are a few approaches:
1. The dependency property
While you using the dependency property it makes the most sense in elements-classes that have a visual appearance (
UIElements).Pros:
Cons:
DependencyObjectSample:
2. The System.ComponentModel.INotifyPropertyChanged
Usually, when creating a data object, you’ll use this approach. It is simple and neat solution for Data-like stuff.
Pros and Cons – complementary to 1. You need to to implement only one event (PropertyChanged).
Sample:
3.PropertyNameChanged
Rising an event for each property with specified name(f.e. NameChanged). Event must have this name and it is up to you to handle/rise them. Similar approach as 2.
4. Get the binding
Using the
FrameworkElement.GetBindingExpression()you can get theBindingExpressionobjectand call
BindingExpression.UpdateTarget()to refresh.First and second are the most likely depending what is your goal.
All in all, it is Visual vs Data.