question

RodAtWork avatar image
0 Votes"
RodAtWork asked ·

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

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?

wpf
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AlexLi-MSFT avatar image
0 Votes"
AlexLi-MSFT answered ·

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 · Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

@AlexLi-MSFT, thank you for your answer. Perhaps I'm not understanding what the original solution was, but to me, having the App object launch child, modal objects only results in the app being the parent. It's the problem we're experiencing, or at least something very similar.

0 Votes 0 · ·
NextstepT-5332 avatar image
0 Votes"
NextstepT-5332 answered ·
· Share
10 |1000 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.