locked
How to host a WPF window within another WPF window RRS feed

  • Question

  • Hi, I'd like to host the content of a WPF window inside another's WPF window's grid cell. Anyone knows how to do it?

    Thursday, August 23, 2007 4:57 AM

Answers

  • Cannot you just do this:

     

    ChildWindow child = new ChildWindow();

    object content = child.Content;

    child.Content = null;

    _grid.Children.Add(content);

     

    Friday, August 24, 2007 11:35 PM

All replies

  • I had some progress on this problem.

     

    public partial class ParentWindow : Window

    {

    public ParentWindow()

    {

    InitializeComponent();

    ChildWindow child = new ChildWindow();

    child.DetachContent();

    // This window has a Grid control named _grid, which is defined in XAML

    this._grid.Children.Add( child.Content );

    }

    }

    public partial class ChildWindow : Window

    {

    public void DetachContent()

    {

    RemoveLogicalChild( Content );

    }

    }

     

    The above code puts the ChildWindow content into the ParentWindow. But I still have 2 issues:

    1. Because RemoveLogicalChild is a protected method. I cannot call it directly from ParentWindow to detach the ChildWindow's content. The content needs to be detached before re-assigned to the ParentWindow. So I have to add DetachContent() public method in ChildWindow to do it. If I cannot change to ChildWindow source code, it could be a problem. I can get around this by using reflection, but it is still a pain.

    2. When I close the ParentWindow after ChildWindow is attached, the program won't terminate! The Main Thread won't exit for some reason.

     

    Please let me know how to get around this problem.

    Friday, August 24, 2007 12:25 AM
  • Cannot you just do this:

     

    ChildWindow child = new ChildWindow();

    object content = child.Content;

    child.Content = null;

    _grid.Children.Add(content);

     

    Friday, August 24, 2007 11:35 PM
  • Thanks for the tip. I wouldn't think it could be this simple but it does put a child window content into the parent window. As for my 2nd problem, I have to manually close the child window when ParentWindow closes.

     

    public partial class ParentWindow : Window

    {

    ChildWindow _child;

    public ParentWindow()

    {

    InitializeComponent();

    _child = new ChildWindow();

    object childContent = _child.Content;

    _child.Content = null;

    _grid.Children.Add(childContent as UIElement);

    }

    protected override void OnClosed(EventArgs e)

    {

    _child.Close();

    base.OnClosed(e);

    }

    }

     

    Saturday, August 25, 2007 1:55 PM
  • Hello,

    You may as well call _child.Close() right after setting _child.Content = null; - the child window is no longer needed after you detach its contents.

    Best regards,
    Bartosz Bien
    Tuesday, April 1, 2008 9:48 PM
  • My only problem is that _child.Content is null.

    _child is not null.

    Also calling _child.show before calling _child.Content still gives_child.Content = null.


    http://www.SethEden.com

    Tuesday, May 28, 2013 3:08 PM
  • I don't think this was ever a design point of WPF to allow this, but good luck..actually why?  Why do you need this?  Doesn't make sense...

    JP Cowboy Coders Unite!

    Tuesday, May 28, 2013 10:50 PM