Click here to Skip to main content
14,643,840 members
Articles » Platforms, Frameworks & Libraries » Windows Presentation Foundation » Controls
Tip/Trick
Posted 16 Jul 2011

Tagged as

Stats

65K views
13 bookmarked

TIP: Make a WPF dialog/window as application modal

Rate this:
4.85 (15 votes)
Please Sign up or sign in to vote.
4.85 (15 votes)
2 Nov 2012CPOL
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()
        {
            InitializeComponent();
            this.Owner = App.Current.MainWindow;
 
            // Any other stuff you want to put here
        }
 
Where App is traditionally the name of the WPF application class.

License

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

Share

About the Author

Jaime Olivares
Architect
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 Studio and TFS.

Comments and Discussions

 
GeneralMy vote of 5 Pin
Eric Dunbar5-Aug-17 10:14
MemberEric Dunbar5-Aug-17 10:14 
GeneralMy vote of 5 Pin
zitun16-Jul-14 3:33
Memberzitun16-Jul-14 3:33 
QuestionBetter practise?!?!? In my opinion anyway... Pin
Johnny J.12-Nov-12 23:02
professionalJohnny J.12-Nov-12 23:02 
AnswerRe: Better practise?!?!? In my opinion anyway... Pin
Jaime Olivares14-Nov-12 10:26
MemberJaime Olivares14-Nov-12 10:26 
GeneralRe: Better practise?!?!? In my opinion anyway... Pin
Johnny J.14-Nov-12 20:59
professionalJohnny J.14-Nov-12 20:59 
GeneralMy vote of 5 Pin
Carsten V2.04-Nov-12 8:18
MemberCarsten V2.04-Nov-12 8:18 
You're my hero! Smile | :)
Thanks, I'll use it!
GeneralReason for my vote of 4 Excellent example, very practical an... Pin
atheneasolucion15-Dec-11 18:19
Memberatheneasolucion15-Dec-11 18:19 
GeneralYes, thanks for the reminder. Pin
Dr.Walt Fair, PE17-Jul-11 6:13
subeditorDr.Walt Fair, PE17-Jul-11 6:13 

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

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