WPF dependency property MVVM

泪湿孤枕 提交于 2019-12-25 05:09:34

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!