问题
I am in the process of creating a user control, this control will do some work and then populate three dependency properties, which will then used by parent elements of the control through binding.
My question is what are the best practices on where to keep the dependency properties using MVVM? Should I use a framework for MVVM?
Thanks
回答1:
Idiomatic dependency properties have nothing to do with 'view state' as represented by the ViewModel and there is no reason to add them to the ViewModel. I would place them in a file that contains the behavior for the control that you are implementing.
回答2:
DependencyProperties
are meant to be used by WPF's binding system, which is what ties the UI layer to the Data layer. They should be kept in the UI layer, and not in the data layer (ViewModels
)
The question about if you should use a framework or not is totally separate.
I would definitely recommend a framework that includes generic things that are frequently used in MVVM (a base class for objects that inherits INotifyPropertyChanged
, a RelayCommand
or DelegateCommand
, a messaging system like EventAggregtor
or Messenger
, etc), however I'd encourage you to take the time to figure out how MVVM works first before using a framework :)
回答3:
While you could implement the MVVM pattern without using a framework why reinvent the wheel? Just use one of the many great MVVM frameworks (Caliburn, MVVMLight, Simple MVVM Toolkit to name a few...).
来源:https://stackoverflow.com/questions/11548952/wpf-dependency-property-mvvm