WPF Window Management and Parenting

I get a lot of questions about what kinds of windows can be created from where in WPF when I give WPF talks. In a talk in Cleveland the other night, there was an attendee who was concerned because he didn’t see a way to set a parent window for another window, and thought that is why they did not include an MDI windowing style in WPF.

If you want to set up a window ownership model in WPF like you did in Windows Forms, you just need to look at the Owner property, instead of Parent. The behavior is not exactly the same, but a lot of the same concepts apply.

If you create a window instance and set its Owner property to a reference to another Window, then when you show the child window it will always be displayed on top of the Owner window. This is typically used when creating an application modal dialog, but it behaves that way even if you show the window with a call to Show vs ShowDialog. The latter just adds the behavior that you cannot select the owner window and interact with it while the dialog is in place. When you call Show on an owned window, the user can still move the window out of the way, click on the owner window and interact with controls in that window. You could do this to create a floating tool palette window for example.

The fact that there is not a built-in implementation of MDI doesn’t mean the framework doesn’t support it, the WPF team just decided to leave that out of scope for the initial (v 3.0) release because very few applications are built from scratch with the MDI model these days.

If you really want MDI, you have two choices:

  • Write it yourself. This would be a little tricky because you would have to detect Window movement and either prevent them from leaving the horizontal or vertical constraints of their owner window’s client area, or you would have to deal with how to do clipping of the contained windows, because WPF does not clip by default.
  • Adopt Acropolis. This framework is coming out in the Orcas timeframe and provides a modular WPF architecture based on concepts carried over from the Composite UI Application Block and the Smart Client Software Factory. It adds better support for inexperienced programmers to use the framework, adds pre-built components, a great design time interaction, and certain other application level support – such as an MDI application model.

Another window management question that often comes up is to ask whether you can use Windows Forms windows or Windows Forms message boxes within your WPF application. The short answer is – yes you can. You can simply construct a Windows Forms Form class and call show and it will just be another top level window presented by your application. Since Windows Forms message boxes really just make an interop call into the OS to display an OS message box, that is of course supported too. However, WPF has its own MessageBox class, so using Windows Forms MessageBoxes is really only interesting while migrating an existing Windows Forms application to WPF.