Good or bad practice for 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 common in WPF applications, but in an MVVM design the important question is not whether dialogs are "good" or "bad." The real issue is whether dialog behavior is implemented in a way that preserves separation of concerns, keeps view models testable, and avoids hard-coding UI dependencies into business logic.
Core Sections
The Core MVVM Rule
In MVVM, a view model should express intent, not directly create windows. If a view model calls new Window() or MessageBox.Show(...) everywhere, it becomes tightly coupled to WPF UI details and harder to unit test.
A cleaner approach is to depend on an abstraction such as IDialogService. The view model asks for a dialog operation, and a WPF-specific service handles the actual window creation.
That keeps the view model focused on decisions and application flow instead of visual behavior.
A Simple WPF Dialog Service
The WPF layer can implement the interface using MessageBox or a custom window.
This is a practical compromise. The application still uses native dialogs, but the view model is no longer responsible for rendering them.
Custom Dialog Windows With View Models
For richer dialogs such as edit forms, file metadata screens, or multistep confirmations, use a dedicated dialog view and dialog view model. The dialog service can map a dialog view model to a WPF window and return a result.
The service might create a RenameFileWindow, assign its DataContext, show it modally, and then return the result. The parent view model only knows that a dialog was requested and whether the user accepted or canceled.
That pattern scales better than pushing every popup through MessageBox, especially when dialogs contain validation, commands, or multiple fields.
Commands and Dialog State
Dialogs fit naturally with commands in MVVM. A command can ask the dialog service to open a window, and if the result is positive, continue the workflow.
The view model owns the data and the decision flow. The service owns the WPF window mechanics.
When Direct Dialog Calls Are Acceptable
For tiny apps, code-behind or a direct MessageBox.Show in a view model may seem harmless. Sometimes that is an acceptable shortcut if the dialog is trivial and the app is unlikely to grow.
Still, once the application has several screens, test coverage, or reusable workflows, direct calls become a maintenance cost. They make mocking harder and scatter UI policy through business logic. If you already know the project matters, it is usually worth introducing a service early.
Common Pitfalls
- Letting a view model create concrete windows or call
MessageBox.Showeverywhere directly. - Returning UI objects from the dialog service instead of simple results or data.
- Building an oversized dialog framework when a small service interface would solve the real problem.
- Treating dialogs themselves as anti-MVVM instead of focusing on whether the UI dependency is isolated properly.
- Forgetting to keep validation, commands, and result handling in the dialog view model instead of pushing everything into code-behind.
Summary
- Dialogs are fine in WPF with MVVM if the view model does not directly manage window details.
- Use a dialog service interface so view models express intent instead of creating UI objects.
- Simple confirmations can wrap
MessageBox; richer flows deserve dedicated dialog views and view models. - Commands are a natural place to trigger dialogs and react to user decisions.
- The bad practice is not "using dialogs"; it is coupling dialog implementation tightly to the view model.
Related reading
- Got Pipelining of requests forbidden in c# rabbitmq client
- GridView must be placed inside a form tag with runatserver even after the GridView is within a form tag
- Group by in LINQ
- Guid.Parse or new Guid - What's the difference?
- Handle cancellation of async method
- Handling Dialogs in WPF with MVVM
- Handling 'Sequence has no elements' Exception
- Handling warning for possible multiple enumeration of IEnumerable

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.