M-V-VM Design Question. Calling View from ViewModel

As Kiff noted:

Views should never be instantiated anywhere "below" the UI layer. VMs exist below that realm, therefore this is not the place to put that logic (as you've already realized).

There will almost always be some UI level event that will indicate the need to create the view. In your example, it might be a row (double) click event on the datagrid. That would be the place to new-up and show your DetailsView window.

You have to realize that M-V-VM is slightly different than other patterns like MVC or MVP. The ViewModel has no direct knowledge of the UI. Opening another view is a view-specific function. The View Model should care less what or how many views are using it's data. I most likely would never open the view via a command.

alt text


Views should never be instantiated anywhere "below" the UI layer. VMs exist below that realm, therefore this is not the place to put that logic (as you've already realized).

There will almost always be some UI level event that will indicate the need to create the view. In your example, it might be a row (double) click event on the datagrid. That would be the place to new-up and show your DetailsView window.


Here's a basic rule of thumb on this.

  • If you are handling local actions in your view, you can intiate from the view model.

  • If it's cross view (like showing a search screen), then either use an EventAggregator pattern (an eventing service) or inject an Application Controller which you invoke methods on, and it in turn displays the search.

Tags:

C#

Wpf

Mvvm