Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

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?

share|improve this question
    
I think your edit belongs in a separate question and needs point you want the recommendations to address and possible ask about alternatives. –  Danny Varod Jan 24 '12 at 13:50
add comment

3 Answers

up vote 4 down vote accepted

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.

share|improve this answer
    
So I need to implement some event handling in the form correct? –  SRKX Jan 23 '12 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. –  Scott Whitlock Jan 24 '12 at 12:36
add comment

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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