Posted in C# 4.0, Silverlight, WPF

Load Content of a Window object to another Control

Recently I have been working with WPF. I was developing a functionality of Child window and I need to load the content of existing windows to that child window control. I got the Child Window control from the CodePlex. I think many of you must be aware of it. There is a project called Extended WPF toolkit. You can find some nice control there. Here is the code to load the content of Window object to a user control (Child Window in my case).

Window2 win = new Window2();
Grid newContent = win.Content as Grid;
win.Content = null;
ChildWin.Content = newContent;

“Window2” is the name of my window that I wanted to open in child window. The root element in the Window2 is Grid, so I cast the content as Grid and assigned it to a local variable of type Grid.

Now, note the use of: win.Content = null;

To use the content of an object we need to disconnect the content from its parent, in this case it is the instance of Window2. This line will do the trick and allow you to use the content the way you like. I have put this content to my child window’s content and that is it.

I got what I want.

You can see above, I have display the content of Window2 to my Child Window control.

I hope this help to someone.