In the MVVM pattern for WPF, handling dialogs is one of the more complex operations. As your view model does not know anything about the view, dialog communication can be interesting. I can expose an ICommand that when the view invokes it, a dialog can appear.
Does anyone know of a good way to handle results from dialogs? I am speaking about windows dialogs such as MessageBox.
One of the ways we did this was have an event on the viewmodel that the view would subscribe to when a dialog was required.
public event EventHandler<MyDeleteArgs> RequiresDeleteDialog;
This is OK, but it means that the view requires code which is something I would like to stay away from.
Why not just raise an event in the VM and subscribe to the event in the view? This would keep the application logic and the view seperate and still allow you to use a child window for dialogs.
After spending some time with it, I finally came up with the following solution. A few key advantages of this approach are:
IDialogService
.PresentationFramework
reference.Here's the implementation of
IDialogService
(goes into ViewModel project):Here's the presentation layer (goes into View project)
I struggled with the same problem. I have come up with a way to intercommunicate between the View and the ViewModel. You can initiate sending a message from the ViewModel to the View to tell it to show a messagebox and it will report back with the result. Then the ViewModel can respond to the result returned from the View.
I demonstrate this in my blog:
I really struggled with this concept for a while when learning (still learning) MVVM. What I decided, and what I think others already decided but which wasn't clear to me is this:
My original thought was that a ViewModel should not be allowed to call a dialog box directly as it has no business deciding how a dialog should appear. Beacause of this I started thinking about how I could pass messages much like I would have in MVP (i.e. View.ShowSaveFileDialog()). However, I think this is the wrong approach.
It is OK for a ViewModel to call a dialog directly. However, when you are testing a ViewModel , that means that the dialog will either pop up during your test, or fail all together (never really tried this).
So, what needs to happen is while testing is to use a "test" version of your dialog. This means that for ever dialog you have, you need to create an Interface and either mock out the dialog response or create a testing mock that will have a default behaviour.
You should already be using some sort of Service Locator or IoC that you can configure to provide you the correct version depending on the context.
Using this approach, your ViewModel is still testable and depending on how you mock out your dialogs, you can control the behaviour.
Hope this helps.
I use this approach for dialogs with MVVM.
All I have to do now is call the following from my view model.
Karl Shifflett has created a sample application for showing dialog boxes using service approach and Prism InteractionRequest approach.
I like the service approach - It's less flexible so users are less likely to break something :) It's also consistent with the WinForms part of my application (MessageBox.Show) But if you plan to show a lot of different dialogs, then InteractionRequest is a better way to go.
http://karlshifflett.wordpress.com/2010/11/07/in-the-box-ndash-mvvm-training/