Take the 2-minute tour ×

I've got a WPF application which calls MessageBox.Show() way back in the ViewModel (to check if the user really wants to delete). This actually works, but goes against the grain of MVVM since the ViewModel should not explicitly determine what happens on the View.

So now I am thinking how can I best implement the MessageBox.Show() functionality in my MVVM application, options:

  1. I could have a message with the text "Are you sure...?" along with two buttons Yes and No all in a Border in my XAML, and create a trigger on the template so that it is collapsed/visible based on a ViewModelProperty called AreYourSureDialogueBoxIsVisible, and then when I need this dialogue box, assign AreYourSureDialogueBoxIsVisible to "true", and also handle the two buttons via DelegateCommand back in my ViewModel.

  2. I could also somehow try to handle this with triggers in XAML so that the Delete button actually just makes some Border element appear with the message and buttons in it, and the Yes button did the actually deleting.

Both solutions seem to be too complex for what used to be a couple lines of code with MessageBox.Show().

In what ways have you successfully implemented Dialogue Boxes in your MVVM applications?

share|improve this question
Similar question: stackoverflow.com/questions/315180/… –  Tiberiu Ana Jul 8 '09 at 13:32

11 Answers 11

up vote 5 down vote accepted

Of the two you mention, I prefer option #2. The Delete button on the page just makes the "Confirm Delete Dialog" appear. The "Confirm Delete Dialog" actually kicks off the Delete.

Have you checked out Karl Shifflett's WPF Line Of Business Slides and Demos? I know he does something like this. I'll try to remember where:


EDIT: Check out Demo #11 "Data Validation in MVVM" (EditContactItemsControlSelectionViewModel.DeleteCommand). Karl calls a popup from the ViewModal (What!? :-). I actually like your idea better. Seems easier to Unit Test.

share|improve this answer
I finally solved this with solution #1. It doesn't pop up a window, just "inserts" a Border element at the top of the screen by triggering it from Visibility=collapsed to Visibility=Visible. I created the DelegateCommands: TurnOnDialogueBoxDelete, DeleteItem and CancelDialogueBoxDelete. I created the ViewModelProperties: DialogueBoxDeleteStatus, ItemIdMarkedForDeletion, and DialogueBoxDeleteText. So it is not as easy as a simple MessageBox.Show() but it works nicely. I'm sure eventually some of this can be abstracted away for when other dialogue boxes are needed. –  Edward Tanguay Jul 8 '09 at 14:41
And it can be unit tested. :-) –  Edward Tanguay Jul 8 '09 at 14:43

why not just do something really simple with Action<> and Func<> http://deanchalk.com/wpf-mvvm-simple-messagebox-show-with-action-func/

share|improve this answer
This is by far the simplest solution I've seen. –  JP Richardson Jun 7 '10 at 15:43
@Dean, can't find this now on your blog? –  Benjol Apr 30 '13 at 7:55
Chinese scrapers to the rescue ;) –  Benjol Apr 30 '13 at 8:29
deanchalk.com/2010/05/06/… –  kbird Jun 4 '13 at 15:55

Services to the rescue. Using Onyx (disclaimer, I'm the author) this is as easy as:

public void Foo()
    IDisplayMessage dm = this.View.GetService<IDisplayMessage>();
    dm.Show("Hello, world!");

In a running application, this will indirectly call MessageBox.Show("Hello, world!"). When testing, the IDisplayMessage service can be mocked and provided to the ViewModel to do what ever you want to accomplish during the test.

share|improve this answer

I would just throw it from the VM. I dont want to have to use someone else's service or write my own just to throw a messagebox.

share|improve this answer
Yeah but if you're testing your view model in a unit test, what's going to happen ? Does your test will block waiting for user to click on 'yes' or 'no' ? –  esylvestre May 19 '10 at 20:21

I've made a simple MessageBox wrapper control for us to use in pure MVVM solution and still allowing unit testing capability. The details are in my blog http://geekswithblogs.net/mukapu/archive/2010/03/12/user-prompts-messagebox-with-mvvm.aspx


share|improve this answer

I just create an interface (IMessageDisplay or similar) which gets injected into the VM, and it has methods like a MessageBox (ShowMessage() etc). You can implement that using a standard messagebox, or something more WPF specific (I use this one on CodePlex some guy called Prajeesh).

That way everything's separated and testable.

share|improve this answer


Check it out here

share|improve this answer

WPF & Silverlight MessageBoxes

MVVM supported


share|improve this answer

What about raising an Event like "MessageBoxRequested" handled in the codebehind of the View (anyway it's View only code so I don't see any problem with having this code on the codebehind).

share|improve this answer

Just in case anyone else is still reading and unsatisfied:

I just wanted to handle 'notification' type MessageBoxes (i.e. I don't care about the DialogResult), but the problem that I have with most solutions I've read about is that they seem to indirectly force you to choose your View implementation (that is, currently I have a MessageBox.Show, but if I later decide to just fiddle with the visibility of a hidden panel directly in my View, that won't mesh very nicely with an INotification interface passed in to the ViewModel).

So I went for quick and dirty:

The ViewModel has a string NotificationMessage property, with changes notified to PropertyChanged.

The View subscribes to PropertyChanged, and if it sees the NotificationMessage property come through, does whatever it wants.

OK, so this means the View has code-behind, and the name of PropertyChanged is hard-coded, but it would be hard-coded in the XAML anyway. And it means I avoid all the stuff like converters for Visibility, and properties to say whether the notification is still visible or not.

(Admittedly this is just for a limited use-case (fire and forget), I haven't given much thought to how I might want to extend it.)

share|improve this answer

I recently came across this problem where I had to replace the MessageBox.Show in the ViewModels with some fully MVVM complaint message box mechanism.

To achieve this I used InteractionRequest<Notification> and InteractionRequest<Confirmation> along with interaction triggers and wrote my own Views for the message box.

What I've implemented is published here

share|improve this answer

Your Answer


