5

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?

1
  • I think your edit belongs in a separate question and needs point you want the recommendations to address and possible ask about alternatives. Commented Jan 24, 2012 at 13:50

3 Answers 3

4

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.

2
  • So I need to implement some event handling in the form correct?
    – SRKX
    Commented Jan 23, 2012 at 20:50
  • @SRKX - anything in the "View" layer can technically create a message box and show it. Your user control that displays the list of messages could do it when it encounters a new message with the flag set. Commented Jan 24, 2012 at 12:36
3

Cinch implements a message box service (among others) that can be mocked for unit testing purposes (or completely replaced by something else). That's what we've been using for the past few months. The ViewModel is in a way requesting something UI-specific, but really it doesn't know how that will be presented to the user, and it's not "talking" to the UI controls (WPF) directly. I've seen a couple of other MVVM frameworks adopting a similar approach.

Unity is for dependency injection (mostly). Cinch uses MEF. Not that you actually NEED that though - The MVVM pattern doesn't rely on that, even though some frameworks sort of do.

0

You could raise a custom command, i.e. AlertUserCommand, then have one or more of your view-models implement the command.

The command will need to be implemented in your main view model.

The implementation of the command can be anything from calling the system message dialog to creating a custom message view + setting its datacontext or setting a property on the view-model that something in the current view is binded to (e.g. a list box or status bar).

You can override the command in lower view-models if they want a different type of alert e.g. display in current control instead of where main vm displays it.

You could use the command parameters to specify severity and have the command implementation depend on the severity or set a vm property with the severity.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.