简体   繁体   中英

WPF dependency property MVVM

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

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 :)

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.

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...).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM