tags:

views:

1269

answers:

7

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?

+3  A: 

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:

http://karlshifflett.wordpress.com/2009/06/17/chicago-wpf-lob-tour-materials-new-materials-added/

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.

Aaron Hoffman
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
And it can be unit tested. :-)
Edward Tanguay
+4  A: 

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.

wekempf
+1  A: 

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.

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
+1  A: 

Hi,

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

mukapu

mukapu
+4  A: 

why not just do something really simple with Action<> and Func<> http://www.deanchalk.me.uk/post/WPF-MVVM-e28093-Simple-e28098MessageBoxShowe28099-With-Action-Func.aspx

Dean Chalk
This is by far the simplest solution I've seen.
JP
A: 

I had a look at several of the suggestions here and would recommend looking at Mukapu's wrapper. This is very lightweight and keeps UI-specific code out of the ViewModel. Great stuff Mukapu!

Pete
A: 

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.

Grant Crofton