1
votes

I know there are many questions regarding how a ViewModel should open a dialog box, how does the Model launch dialogs?

Should the model call a dialog service, that call the viewmodel's dialog service, all the way up to the view? Should the model have a reference to the mvvm-light toolkit?

Specifically for my situation - when my model is passed some data to restore / interpret and build model objects (I think this should be the model's job) - there is potential for errors to be raised.


Update #1: I've accepted the answer below, based on the comments from fmunkert. I've realized that I was asking the wrong question, and the fundamental problem is designing a paradigm for the model to signal errors.

2

2 Answers

2
votes

Since MVVM is not a standard and since there is no definitive authority that decides what is correct in MVVM and what is not, you can implement dialog boxes in any way you see appropriate, as long as you do not call any WPF dialogs directly from the ViewModel or model.

In the applications I recently wrote, I used the following two approaches (with my own MVVM framework library):

  • Modal dialogs are called from the ViewModel via a "service". I.e. the ViewModel has a way to obtain an IFrontend pointer with a ShowModalDialog() method. There are two generic implementations for IFrontend: one for WPF (which opens a dialog) and one for the unit test environment (which just simulates a dialog).

  • Non-modal dialogs sometimes can be opened without the ViewModel knowing about them. E.g. if you have a ViewModel for a form where you need to be able to open a font selection dialog, then this is a user interface detail and the ViewModel does need to know about that detail. The ViewModel does not care whether the font is selected using a dialog or using a drop-down list.

If you are using third-party libraries like MVVM Light or Prism, you probably should follow the recommendations from the library's documentation.

0
votes

I'm not sure if you are still looking for any help, but the approach that I have taken when it comes to dialogs is to have the view model raise an event that the view can then handle. The view can now do whatever it wants to get the data to the view model, so you can disply the dialog in the view without a problem. You pass the response from the dialog to the EventArgs of your event so that the view model has the data it is looking for in order to proceed.

For example:

Public Class View

   Private WithEvents _VM AS new ViewModel()

   Private Sub _VM_AddingItem(Sender AS Object, E AS ViewModel.ItemEventArgs)
      Dim Dialog As new SomeDialog()

      If Dialog.ShowDialog then 
         E.Item = Dialog.Item
      Else
         E.Cancel = True
      End If
   End Sub 

End Class


Public Class ViewModel 
   Public Sub AddItem(Item AS Object) 
       Do Some Work here 
    End Sub 

    Private Sub _AddItem() 
       Dim Args AS New ItemEventArgs()

       OnAddingItem(Args)

       If not Args.Cancel Then AddItem(Args.Item)
    End Sub 

    Protected Sub OnAddingItem() 
       RaiseEvent AddingItem(me, ItemEventArgs)
    End Sub

    Public Event AddingItem(Sender AS Object, E As ItemEventArgs)

    Public Class ItemEventArgs
       Public Property Item AS Object
       Public Property Cancel AS Boolean = false
    End Class
End Class

Then just wire up your command to the private _AddItem method which just raises the event to collect the necessary data for the AddItem method. I hope this helps :)