How to Use a MessageBox with the MVVM Pattern in C#

cdesign-patternsmvvm

I am implementing a UI application using the MVVM design-pattern.

The thing is, sometimes when you detect that something is wrong with data that was inputted by the user, you'd like to notify him somehow.

The solution I used so far was to have a "messaging framework" which allows me to basically show messages in a ListView I integrate within the window.

However, sometimes the windows get a bit "overloaded" with graphical elements, and I would like to be able to show the messages in a message box (basically a modal window).

As the ViewModel is not supposed to be aware of anything about the UI, I cannot instantiate any window "explicitly".

Ideally, this modal window would also use the MVVM pattern and hence I would implement a ViewModel for that window, which would be instantiated within the "main" windows's ViewModel, and passed to the modal window through the DataContext.

I would like to have a "simple" example of how to do such thing? Or should this approach be banned from an MVVM application?

EDIT:

Would the use of the Unity Framework be something you recommend?

Best Answer

Make it part of your messaging framework. That is, your message could have an attribute such as UserMustAcknowledge. When the View of the messaging framework sees that, it kicks off a message box.