Simple way of showing Msgbox and Dialogs in WPF MVVM

I know that MVVM purists say that you should not use messageboxes and Dialogs in a MVVM application , however as a business developer you must sometimes blok the GUI to let the user pick an item or confirm there actions . After some discussions in the comunity i decided to show y

4 Star
3,564 times
Add to favorites
E-mail Twitter Digg Facebook

Solution explorer

'Michel Posseth 26-03-2011
Namespace VBDC.ViewModel
    Public Class MainWindowViewModel
        Inherits ViewModelBase
        Public Sub New(ByVal e As StartupEventArgs, ByVal myWindow As Window)
            Me.DisplayName = "Main view"
            Me.myWindow = myWindow
            AddHandler MyBase.eShowMsgbox, AddressOf Me.MsgboxConfirmation
            ' Allow all controls in the window to 
            ' bind to the ViewModel by setting the 
            ' DataContext, which propagates down 
            ' the element tree.
            myWindow.DataContext = Me
        End Sub
        Private _CloseCommand As ICommand
        Public ReadOnly Property CloseCommand As ICommand
                If _CloseCommand Is Nothing Then
                    _CloseCommand = New RelayCommand(AddressOf CloseCommandExecute)
                End If
                Return _CloseCommand
            End Get
        End Property
        Private Sub CloseCommandExecute(ByVal ByvalParamNotUsed As Object)
        End Sub
        Private _MsgboxDemoCommand As ICommand
        Public ReadOnly Property MsgboxDemoCommand As ICommand
                If _MsgboxDemoCommand Is Nothing Then
                    _MsgboxDemoCommand = New RelayCommand(AddressOf MsgboxDemoCommandExecute)
                End If
                Return _MsgboxDemoCommand
            End Get
        End Property
        Private _DialogDemoCommand As ICommand
        Public ReadOnly Property DialogDemoCommand As ICommand
                If _DialogDemoCommand Is Nothing Then
                    _DialogDemoCommand = New RelayCommand(AddressOf DialogDemoCommandExecute)
                End If
                Return _DialogDemoCommand
            End Get
        End Property
        Private Sub DialogDemoCommandExecute(ByVal ByvalParamNotUsed As Object)
            ' some might argue that a Dialog is not MVVM 
            ' to the view from code 
            ' however you can stil unit test the models so i do not see the problem
        End Sub
        Private Sub ShowDialog()
            Using viewModel = New DialogViewModel(New MainWindow)
                AddHandler viewModel.eShowMsgbox, AddressOf MsgboxConfirmation
                AddHandler viewModel.eShowAnotherDialog, AddressOf ShowDialog
            End Using
          End Sub
        Private Sub MsgboxDemoCommandExecute(ByVal ByvalParamNotUsed As Object)
            ' this is a demo to show how to invoke on the main view model 
            ' however it is also possible to do this on a dialog view from a sub model
            ' As We will demonstrate on the dialog window on tab 2  
            RaiseEventShowMsgbox("Are you sure you wish to proceed", "Example", MessageBoxButton.YesNo, MessageBoxImage.Question, myWindow, Me)
            ' note that the property MsgboxResult could also be set fro automated code 
            ' if there is not a object pointer or a window to send the msgbox to then this is non bloking code !
            ' so a unit test could just set the MsgboxResult to the tested situation 
            Select Case MsgboxResult
                Case MessageBoxResult.Yes
                    ' invoke method on yes situation 
                Case MessageBoxResult.No
                    ' invoke method on no situation 
            End Select
            Debug.WriteLine(String.Concat("You pressed:=", MsgboxResult.ToString))
        End Sub
        ''' <summary>
        ''' This is the one method that invokes the messagebox for all viewmodels 
        ''' </summary>
        ''' <param name="Txt"></param>
        ''' <param name="Caption"></param>
        ''' <param name="ButtonStyle"></param>
        ''' <param name="ImageStyle"></param>
        ''' <param name="myWindow"></param>
        ''' <param name="sender"></param>
        ''' <remarks></remarks>
        Private Sub MsgboxConfirmation(ByVal Txt As String, Caption As String, ByVal ButtonStyle As MessageBoxButton, ImageStyle As MessageBoxImage, ByVal myWindow As Window, ByVal sender As Object)
            Dim MySender = TryCast(sender, IMsgBoxSendRec)
            If myWindow Is Nothing Then myWindow = Me.myWindow
            If MySender IsNot Nothing AndAlso myWindow IsNot Nothing Then
                MySender.MsgboxResult = MessageBox.Show(myWindow, Txt, Caption, ButtonStyle, ImageStyle)
            End If
        End Sub
    End Class

End Namespace