Monday, October 19, 2009

WPF and the modal dialog (boring)

Warning boring tech related blog entry follows:

Holy cows! WPF has made it VERY difficult to create a modal dialog box. It's difficult, because to do it properly, your modal dialog has to have a pointer to it's parent window. You can skip this step, have have one of those really junky applications that hide away your modal dialog in a land far far away.

The trick (hack) is the following

1) You need a System.IntPtr containing a pointer to the windows handle (this.Handle if you are lucky enough to be in a System.Windows.Window.Form)

2) You need to feed this pointer into your dialog thusly:

WindowInteropHelper wih = new WindowInteropHelper(myUserControl);
wuh.Owner = myIntPtr;
// then, with magical flair:
myUserControl.ShowDialog();

No comments: