manav inder
manav inder

Reputation: 3601

How a ViewModel communicates with its own View

I am developing a WPF MVVM Light application, I want to display some message boxes/dialog boxes or there are some other scenarios where I want my View model to communicate with its view or raise some event on its respective view.

How could I achieve that?

Upvotes: 2

Views: 195

Answers (3)

Patrice Calvé
Patrice Calvé

Reputation: 694

The responsibility of displaying the popup should not be the ViewModel's responsibility, but a "service that takes care of it for you".

If the "popup window" is a modal like window like a "file open dialog", "file save dialog", message box "ok/cancel" or "yes/no" etc, then I believe that a "service" would be the right choice. The reason is that the service can be facked/mocked for unit testing.

Also, you can have different implementations of this service based on whatever you want: in debug (r, when the role is of type admin or developer) you have more "verbose" information than in release or regular users, for example.

Now, for "how does a ViewModel communicate to the View", I'm not sure what you mean. Simply add a property on the ViewModel and bind the view to that property.

Upvotes: 1

Erti-Chris Eelmaa
Erti-Chris Eelmaa

Reputation: 26298

See Messenger class of MVVM Light. It should even have sample by default when you downloa MVVM light. It's something like Messenger.Default.Send(new YourMessageClass()) and you can register listener on View side.

Upvotes: 2

m0sa
m0sa

Reputation: 10940

The prism framework has a concept of interaction requests to solve this problem. A nice example can be found here or here.

Upvotes: 1

Related Questions