Click here to Skip to main content
Click here to Skip to main content

Tagged as

TIP: Make a WPF dialog/window as application modal

, 2 Nov 2012 CPOL
Rate this:
Please Sign up or sign in to vote.
Generic tip to make a WPF window modal to the application's main window
When you create a new window from inside your application, like in this example:
  var dialog = new MyDialog();
  if (dialog.ShowDialog().Equals(true))
      // do something here
if you press Alt-tab when the dialog is open, and you come back to the application pressing Alt-tab again, the main window will be shown but not the dialog. The reason is that the dialog has not been declared as owned by the main window.
The solution for this undesired behaviour is very simple: Inside the dialog constructor, right after the invocation to InitializeComponent(), set the owner of the dialog, which is the main window:
        public MyDialog()
            this.Owner = App.Current.MainWindow;
            // Any other stuff you want to put here
Where App is traditionally the name of the WPF application class.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Jaime Olivares
Architect Freelance (
Peru Peru

Computer Electronics professional, Software Architect and senior Windows C++ and C# developer with experience in many other programming languages, platforms and application areas including communications, simulation systems, PACS/DICOM (radiology), GIS, 3D graphics and HTML5-based web applications.
Currently intensively working with Visual C# 2013 and TFS.
Follow on   LinkedIn

Comments and Discussions

GeneralReason for my vote of 4 Excellent example, very practical an... Pinmemberatheneasolucion15-Dec-11 19:19 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.150327.1 | Last Updated 2 Nov 2012
Article Copyright 2011 by Jaime Olivares
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid