How to make modal dialog in WPF?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In WPF, a modal dialog is simply a Window shown with ShowDialog(). While it is open, the user cannot interact with the owner window, which makes it appropriate for confirmations, short forms, and choices that must be resolved before the main flow can continue.
Build the Dialog as a Normal Window
Start by creating a WPF window for the dialog content. A modal dialog is not a special XAML type; it becomes modal only when you display it modally.
This gives you a focused dialog window with two actions.
Return a Result With DialogResult
In the code-behind, setting DialogResult both communicates the outcome and closes the dialog.
This is the normal pattern for simple yes-or-no dialogs in WPF.
Show It Modally From the Owner Window
The dialog becomes modal when the parent window calls ShowDialog().
The Owner assignment matters. It keeps focus behavior correct, centers the dialog relative to the parent, and helps avoid the dialog appearing behind the main window.
Pass Data Into the Dialog
Real dialogs often need to receive state from the caller. Constructor arguments or settable properties work well for this.
Then call it like this:
That keeps the dialog self-contained while still allowing it to exchange data with the caller.
Keep the Dialog Focused
Modal dialogs should stay small and specific. They work well for:
- confirm or cancel decisions
- short data entry
- pick-one choices
- warnings that must be acknowledged
They are a poor fit for long workflows, complex navigation, or anything that feels like a full screen disguised as a popup. If the user needs a lot of context or several steps, a dedicated page or modeless window is usually better.
MVVM Considerations
In MVVM applications, it is common to keep the dialog UI thin and move the actual business decision into a view model or dialog service. Even if you use code-behind for DialogResult, the dialog should not become a dumping ground for application logic.
The important idea is separation: the window gathers input and returns a result, while the application decides what that result means.
Common Pitfalls
The most common mistake is calling Show() instead of ShowDialog(). Show() opens a normal modeless window and does not block the owner.
Another issue is forgetting to set the dialog owner. Without an owner, focus and positioning behavior can feel sloppy, especially on multi-window desktops.
Developers also sometimes put too much logic into the dialog code-behind. That makes reuse and testing harder.
Finally, avoid overusing modal UI. A dialog that blocks the user should earn that interruption by being short and necessary.
Summary
- In WPF, a modal dialog is just a
Windowshown withShowDialog(). - Use
DialogResultto close the dialog and return success or cancel. - Set the
Ownerfor better focus and positioning behavior. - Pass data in through constructors or properties and read results after the dialog closes.
- Keep modal dialogs short, focused, and light on business logic.
Related reading
- How to make OleDb code run asynchronous?
- How to make String.Contains case insensitive?
- How to make this asynchronous? async, await - C, MVC
- How to make Visual Studio not put on a new line?
- how to manage an NDC-like log4net stack with async/await methods? per-Task stack?
- How to measure the total memory consumption of the current process programmatically in .NET?
- How to modify a KeyValuePair value?
- How to open an Excel file in C?

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.