Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

TIP: Make a WPF dialog/window as application modal

0.00/5 (No votes)
2 Nov 2012 1  
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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here