WPF
Modal Dialog
C#
User Interface
Programming Tutorial

How to make modal dialog in WPF?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

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.

xml
1<Window x:Class="DemoApp.ConfirmDialog"
2        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
3        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
4        Title="Confirm Delete"
5        Height="180"
6        Width="320"
7        ResizeMode="NoResize"
8        WindowStartupLocation="CenterOwner">
9    <Grid Margin="16">
10        <Grid.RowDefinitions>
11            <RowDefinition Height="*" />
12            <RowDefinition Height="Auto" />
13        </Grid.RowDefinitions>
14
15        <TextBlock Text="Delete the selected file?"
16                   FontSize="16"
17                   TextWrapping="Wrap" />
18
19        <StackPanel Grid.Row="1"
20                    Orientation="Horizontal"
21                    HorizontalAlignment="Right"
22                    Margin="0,16,0,0">
23            <Button Content="Cancel"
24                    Width="80"
25                    Margin="0,0,8,0"
26                    Click="Cancel_Click" />
27            <Button Content="Delete"
28                    Width="80"
29                    Click="Delete_Click" />
30        </StackPanel>
31    </Grid>
32</Window>

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.

csharp
1using System.Windows;
2
3namespace DemoApp
4{
5    public partial class ConfirmDialog : Window
6    {
7        public ConfirmDialog()
8        {
9            InitializeComponent();
10        }
11
12        private void Delete_Click(object sender, RoutedEventArgs e)
13        {
14            DialogResult = true;
15        }
16
17        private void Cancel_Click(object sender, RoutedEventArgs e)
18        {
19            DialogResult = false;
20        }
21    }
22}

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().

csharp
1private void DeleteButton_Click(object sender, RoutedEventArgs e)
2{
3    var dialog = new ConfirmDialog
4    {
5        Owner = this
6    };
7
8    bool? result = dialog.ShowDialog();
9
10    if (result == true)
11    {
12        MessageBox.Show("File deleted.");
13    }
14}

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.

csharp
1public partial class NameDialog : Window
2{
3    public string EnteredName => NameTextBox.Text;
4
5    public NameDialog(string currentName)
6    {
7        InitializeComponent();
8        NameTextBox.Text = currentName;
9    }
10}

Then call it like this:

csharp
1var dialog = new NameDialog("Existing value")
2{
3    Owner = this
4};
5
6if (dialog.ShowDialog() == true)
7{
8    string updatedName = dialog.EnteredName;
9}

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 Window shown with ShowDialog().
  • Use DialogResult to close the dialog and return success or cancel.
  • Set the Owner for 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.

Course illustration
Course illustration

All Rights Reserved.