Handling Dialogs in WPF with MVVM
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Dialogs are one of the first places where a WPF application can drift away from clean MVVM. It is easy to call MessageBox.Show or new SomeWindow().ShowDialog() directly from a view model, but that ties application logic to UI classes and makes testing painful. A better approach is to keep dialog behavior behind an interface and let the view layer implement the actual window logic.
Why Dialogs Feel Awkward in MVVM
MVVM works because the view model does not know about concrete controls. It exposes state and commands, and the view binds to them. Dialogs complicate that arrangement because showing a dialog is a UI action with UI-specific concerns such as ownership, modality, focus, and return values.
If a view model creates windows directly, a few problems appear immediately:
- Unit tests now need WPF infrastructure.
- The view model becomes hard to reuse outside a desktop window.
- Owner windows, startup positions, and dialog results get scattered through business logic.
The goal is not to avoid dialogs. The goal is to move dialog creation to a service that the view model can call through an abstraction.
A Simple Dialog Service
The smallest useful pattern is an IDialogService interface. The view model depends on the interface, and the application provides a WPF-specific implementation.
The view stays simple because it only binds to the command and state:
This example is intentionally modest. The key idea is that the view model asks for a confirmation, not for a MessageBox. That distinction is what preserves testability.
Moving to Custom Dialog Windows
Once the service boundary exists, switching from a message box to a custom dialog is straightforward. The service can create a window, assign its DataContext, set Owner = Application.Current.MainWindow, and return ShowDialog().
That gives you a clean place to handle details such as:
- mapping a dialog view model to a dialog window
- centering on the owner window
- passing initial data into the dialog
- reading a typed result after the dialog closes
For example, an edit dialog can expose properties such as CustomerName and IsConfirmed. The service opens the window, and the main view model only receives the result. The main view model still does not need to know whether the UI was a modal window, a sheet-style experience, or a custom host control.
Testing the ViewModel
The service pattern pays off when you test command logic. A fake implementation can return a predetermined answer without spinning up WPF.
With that fake, a unit test can verify that Status becomes "Record deleted." when the answer is true, and "Delete cancelled." when the answer is false.
Common Pitfalls
- Calling
ShowDialog()directly in the view model. This is the most common MVVM leak and usually the hardest one to unwind later. - Forgetting the owner window. Without
Owner, modal behavior, focus restoration, and taskbar stacking can feel wrong. - Returning raw UI types from the service. Keep the contract focused on domain-friendly results such as
bool, strings, or a dedicated result object. - Using the dialog service for long-running work. Dialogs should collect a decision or some input; background operations should still live elsewhere.
- Hiding validation inside the window code-behind. Validation rules belong in the view model so the dialog can be tested like the rest of the screen.
Summary
- MVVM does not prevent dialogs; it requires dialog behavior to be abstracted.
- An
IDialogServicekeeps view models free ofWindowandMessageBoxdependencies. - The same pattern works for both simple confirmations and custom modal windows.
- Setting dialog ownership and returning typed results improves application behavior and maintainability.
- Fake dialog services make command logic easy to unit test.
Related reading
- Handling 'Sequence has no elements' Exception
- Handling warning for possible multiple enumeration of IEnumerable
- Hangfire Background Job with Return Value
- Has this usage of async / await in C been discovered before?
- `Hash` Password in C? Bcrypt/PBKDF2
- HashSetT versus DictionaryK, V w.r.t searching time to find if an item exists
- Haskell equivalent of C 5 async/await
- Hidden Features of ASP.NET

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.