WPF Workaround to add Window control as a child of visual

9.8k Views Asked by At

I have a requirement to add a Window control inside a panel.

Of course this doesn't seem possible: Window must be the root of the tree. Cannot add Window as a child of Visual.

The problem boils down to drawing the Window Chrome (control box, title bar, minimize/restore/close button and borders) inside a panel using the OS theme configured by the user. Are there any workarounds I should be investigating?

2

There are 2 best solutions below

5
On

This is what User Controls are for - creating controls that can be reused in multiple windows. Refactor your Window Control so that it is just a Window that contains a single User Control (which in turn contains everything your window previously had). Place the User Control in your Panel. The idea is that your current window (that you want to be a child, we'll call it Window B) looks something like this:

Window B -> Grid/Panel -> Other Controls

And should instead look like this

Window B -> Grid/Panel -> User Control B -> Grid/Panel -> Other Controls

And now other other window can look like this:

Window A -> Grid/Panel -> ... -> Panel -> User Control B


If you're trying to achieve an MDI like interface, you have a few options - use tab controls, use a panel that can be dragged and/or resized, etc. In this case, you would still want a User Control, so that it could easily be reused in different windows (or panels) that you create, especially if you end up having to create those panels programatically (e.g. if you intend to create multiple instances dynamically at runtime).

In this case, you'll probably want two user controls: one that has all the controls from your other window, and one that would act as a "window" control within your main window. The "Window" control would have functionality for resizing, docking, etc.

WPF does not support creating a window as if it were a control, or as a child of another window (the closest you can get to that is having a window be shown as a modal dialog).

0
On

As the only answer is completely off topic, here is the best answer I can come up with: Create window, render window as Bitmap, put bitmap inside panel.