Show dialog with MVVM Light toolkit

Tejash picture Tejash · Apr 13, 2011 · Viewed 20.7k times · Source

I have a ViewModel that needs to show a modal window (using ShowDialog()) on a button click. The ViewModel catches the click command, but I don't want to do window.ShowDialog() within my ViewModel. I know there is a DialogMessage in MVVM Light, but that is used to show message boxes, not WPF modal windows.

Any ideas on how to do this?

Answer

Rafal Spacjer picture Rafal Spacjer · Apr 13, 2011

You should use Messenger class. On the View register a message to show window, and then when you need to show it call Send method of Messenger class.

You can do something like this:

    //do this in the code-behind file of your View
    Messenger.Default.Register<string>(this, ShowWindow);
    
    private void ShowWindow(string message)
    {
        // your logic here
    }
    
    // In the ViewModel
    Messenger.Default.Send(“Some text”);