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
(1)
3,564 times
Add to favorites
3/26/2011
E-mail Twitter del.icio.us Digg Facebook

Solution explorer

VB.NET
'Michel Posseth 26-03-2011
Namespace VBDC.ViewModel
    Public Class DialogViewModel
        Inherits ViewModelBase
        Private _CloseCommand As ICommand
        Public ReadOnly Property CloseCommand As ICommand
            Get
                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)
            Debug.WriteLine(String.Concat("Closing dialog", " Note this comes from:", Me.DisplayName))
            myWindow.Close()
        End Sub
        Private _MsgboxDemoCommand As ICommand
        Public ReadOnly Property MsgboxDemoCommand As ICommand
            Get
                If _MsgboxDemoCommand Is Nothing Then
                    _MsgboxDemoCommand = New RelayCommand(AddressOf MsgboxDemoCommandExecute)
                End If
                Return _MsgboxDemoCommand
            End Get
        End Property
        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, " Note this comes from:", Me.DisplayName))
        End Sub
        Private _DialogDemoCommand As ICommand
        Public ReadOnly Property DialogDemoCommand As ICommand
            Get
                If _DialogDemoCommand Is Nothing Then
                    _DialogDemoCommand = New RelayCommand(AddressOf DialogDemoCommandExecute)
                End If
                Return _DialogDemoCommand
            End Get
        End Property
        Public Event eShowAnotherDialog()
        Private Sub DialogDemoCommandExecute(ByVal ByvalParamNotUsed As Object)
            Debug.WriteLine(String.Concat("Ask the main Model to create anew dialog", " Note this comes from:", Me.DisplayName))
            RaiseEvent eShowAnotherDialog()
        End Sub
        Public Sub New(ByVal myWindow As Window)
            Me.DisplayName = String.Concat("Dialog window ", Application.Current.Windows.Count - 1)
            Me.myWindow = myWindow
            ' 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
        Public Sub Show()
            myWindow.ShowInTaskbar = False
            myWindow.ResizeMode = ResizeMode.NoResize
            myWindow.ShowDialog()
        End Sub
    End Class
End Namespace