Skip to main content
4 of 4
added 4 characters in body
Sam
  • 123
  • 8

Invoke model logic in one UserControl from view-model in another UserControl without violating MVVM

Here is WPF application consisting from 3 UserControls:

enter image description here

UserControl3 is a part of UserControl2 content. I keep MVVM during developing and using Prism.

I need to invoke custom class method (which is model in terms of MVVM) in UserControl3 from view-model of UserControl1. The restriction that service in UserControl3 can't be singleton. I suppose to do it one of the following way:

  1. Using event aggregator from Prism. UserControl1 view-model is publisher and UserControl3 model is subscriber. For this I'll need to create unique Id in Window and pass it to UserControl1 and UserControl3.

  2. Creating service instance in Window and pass it to UserControl1 and UserControl3. Then UserControl1 just invoke method on this instance.

  3. Window pass UserControl2 instance to UserControl1. View-model in UserControl1 will just invoke method of UserControl2, which will invoke method of UserControl3 and so on.

It seems like 2 and 3 approaches violates MVVM. What will you prefer?

Sam
  • 123
  • 8