Friday, January 11, 2013

WPF ChildWindow

I always wondered what could be the use of the ChildWindow in WPF? Last week I discovered a good reason to use it.
When you create a new window from inside your application, like in this example:
var dialog = new MyDialog();

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.

I found a solution on CodeProject mentioning to set the owner of the Dialog window to the MainWindow like this:

public MyDialog()

 this.Owner = App.Current.MainWindow;

An alternative solution can be found in the Extended WPF Toolkit which contains a ChildWindow class. This class offers the same behavior without the extra code.


No comments: