How to show a WPF dialog using MVVM in C# with a mediator

David Anderson · 48K views · 2017 · 21:53

Open on YouTube

Software architecture & designWeb developmenttutorial

Why Stack Overflow recommends it

Stack Overflow users link it as a worked example of a DialogService for opening dialogs from view-models in WPF MVVM, and several then ask follow-up questions about wiring it up.

Recommended by 1 people across 1 different months, 2017 to 2021.

What it is

A 22-minute tutorial by David Anderson showing how to open a WPF dialog from a view-model under MVVM. It builds a mediator/dialog service so the view-model never touches view classes directly, with downloadable code.

What people said

Recommends David Anderson · 2017-01 · Stack Overflow · on “Separate project for view models to "enforce" MVVM: How do I open dialogs?”

You may want to watch my recent video on working with dialogs in MVVM ( [this video] ). It is a common problem, but can be solved relatively easy. You will still need to decide how you want to inject the services (or not), because you can also use a service locator pattern or other methods. Injection isn't the only way.

Cites Tatjana Cerovic · 2019-03 · Stack Overflow · on “Open dialog on already opened dialog MVVM”

I watched a great video [this video] about creating a DialogService that will take care of opening dialogs in MVVM. Since DialogService constructor takes owner (Window type) as parameter, it is easy to create dialogService in MainWindow. Just create a View and a ViewModel for MainWindow separately in App.xaml.cs and connect all thing. But the problem is how do I create a dialogService in dialog that is opened from the MainWindow (to open more dialogs from that dialog) since I cannot get the View instance of the dialog that is opened from MainWindowViewModel. Here's a method for showing that dialog. private void ShowAdministrationDialog() { AdministrationDialogViewModel viewModel = new Administrat

Cites Kevin Burton · 2021-04 · Stack Overflow · on “DialogService with no dependency on MainWindow”

I tried this [this video] but it is either my misunderstanding or a problem with the design but it seems to result in a circular dependency. As show the DialogService depends on the MainWindow and the MainWindow has a binding to the MainWindowViewModel that uses the DialogService . So the DialogService needs to be constructed after the MainWindow and the MainWindow needs the DataContext set after it is constructed. So the question is how do I build a DialogService with no dependency on Window ?