Cornel Marian
Cornel Marian

Reputation: 2503

Can a ViewModel have a child that is also a ViewModel?

Can a ViewModel have a child (property) that is also a ViewModel in the MVVM pattern used to develop WPF applications?

Upvotes: 1

Views: 108

Answers (2)

Santin
Santin

Reputation: 176

You can have a main ViewModel with other ViewModels, it is important to always set the datacontexts.

Upvotes: 1

Fede
Fede

Reputation: 44068

Yes, it's perfectly acceptable to have a hierarchical relationship (Parent / Child) between your ViewModels.

MainViewModel
  -> ViewModel 1
      -> ViewModel A
  -> ViewModel 2
      -> ViewModel B

These will usually reflect the hierarchical structure of the UI:

MainWindow
  -> UserControl 1
      -> UserControl A
  -> UserControl 2
      -> UserControl B

Upvotes: 2

Related Questions