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

Rod At Work 866 Reputation points
2020-03-02T20:47:26.617+00:00

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 />  
</DataTemplate>  

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,667 questions
0 comments No comments
{count} votes

Accepted answer
  1. Alex Li-MSFT 1,096 Reputation points
    2020-03-03T06:30:00.93+00:00

    Hi,

    Welcome to our Microsoft Q&A platform!

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

    the-best-approach-to-create-new-window-in-wpf-using-mvvm

    standard-approach-of-launching-dialogs-child-windows-from-a-wpf-application-usin

    Thanks.


1 additional answer

Sort by: Most helpful
  1. Nextstep T 1 Reputation point
    2020-03-03T07:13:54.387+00:00
    0 comments No comments