What is the best approach for launching a modal window, in WPF?

Rod At Work 821 Reputation points

Where I work we're having a discussion trying to determine which is the best approach for launching a modal window in WPF, using the regular .NET Framework. (We're using .NET 4.5.2 for this project.) It is a new project that we're developing.

We define all windows, regular or modal, as usercontrols. We've got a resource dictionary where we define the relationship between the view and the viewmodel, like this:

<DataTemplate DataType="{x:Type vm:ProgramSelectViewModel}">  
	<view:ProgramSelectView />  

We're using MVVM Light's Messaging to send messages and information back and forth. The approach we all agree on is having some button on a window which is what the user will click in order to launch the modal window. The difference is where the launching of the modal window is done.

Some of us send a MVVM Light message from the ICommand in the viewmodel associated with the window where the button is. That sends a MVVM Light message, which is caught by another viewmodel, which, at least as far as I understand it, becomes the parent of the modal window, because it launches the view, using the idea in the XAML above.

Others of us (and I'm one of them), think that it makes more sense to send a message from the parent viewmodel, to the parent's code-behind. From the code-behind it instantiates the child view and assign it to the content of a new window, which gets launched from the code-behind. By the same relationship in the XAML above, the viewmodel is also instantiated and if there's any additional information required by the viewmodel, that's passed by another MVVM Light message. This way, the parent's view is the parent of the modal window.

Both approaches seem to work. What I'd like to know is what approach is most commonly used?

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,272 questions
No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,086 Reputation points


    Welcome to our Microsoft Q&A platform!

    You can refer to these two links below about how to create or launch model windows.




1 additional answer

Sort by: Most helpful