0

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

Ali
  • 309
  • 1
  • 5
  • 20
  • Yes, use a framework for MVVM. I like MVVM-Light. – Ritch Melton Jul 18 '12 at 19:42
  • You might be interested in this question if you're looking into MVVM frameworks: [What framework for MVVM should I use?](http://stackoverflow.com/q/1409553/302677). Personally I use Microsoft Prism, however occasionally I'll use parts of MVVM light (such as the `RelayCommand` when I want a Command that automatically re-evaluates `CanExecute()` when properties change) – Rachel Jul 18 '12 at 20:03
  • The dependency property framework is called dependency properties. –  Jul 19 '12 at 13:16

3 Answers3

4

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

Rachel
  • 130,264
  • 66
  • 304
  • 490
2

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.

Ritch Melton
  • 11,498
  • 4
  • 41
  • 54
  • I think you are confusing MVVM with dependency properties for WPF controls. I would place the code in a file that is named after the control you are creating. The term code-behind is usually reserved for connecting up to specific objects. – Ritch Melton Jul 18 '12 at 19:48
0

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

Dean Kuga
  • 11,878
  • 8
  • 54
  • 108
  • I have to inherit my view model from DependencyObject, is that a good practice? – Ali Jul 18 '12 at 19:45
  • I think the properties in question are DPs related to the state of the user control, not model-related properties that would go on the VM. Meaning that they're strictly view-only properties, and belong on the UC itself. @Ali - your VM should not need to inherit from DependencyObject. – Esoteric Screen Name Jul 18 '12 at 19:51