How to Create a New View in MVVM?

Let’s say I have a view that must open another window on command. Where would this code sit?

The model? Don’t be ridiculous.

The view model? Yes, it’s where the command handlers are, but view model is not supposed to know about the views, and this would not be testable.

The View? Well, maybe, but aren’t the command handlers in view model? Besides, the view may not have enough data to create the desired window.

I realized, that all WPF apps that I created so far had pretty much static UI defined at startup, and any simple dialogs that needed to be shown dynamically were created by a “dialog service”.

Surprisingly, quick googling gives very little insight on the topic, although it comes with with this answer and this article.

They seem to advise to create the views in a special layer (“controller”), talking to it via a service locator or an event broker. This kind of makes sense, because none of the 3 components of MVVM is well equipped for making new views. On the other hand, it is annoying, since it means one more moving part.

Posted in

Leave a Reply

Your email address will not be published. Required fields are marked *