I have a window that have a user control. This user control has a button which maximize the user control to full screen.
For achieving this I have created a temporary window which I scale to current screen bounds and set it content to the user control.
private void OnBtnMaximizeClick(object sender, RoutedEventArgs e)
{
if (this.btnMaximize.IsChecked == true)
{
if (tempfullScreenWindow == null)
{
tempfullScreenWindow = new Window();
tempfullScreenWindow.WindowStyle = WindowStyle.None;
tempfullScreenWindow.ResizeMode = ResizeMode.NoResize;
}
var ownerWindow = Window.GetWindow(this);
Screen screen = this.GetContainingScreen(ownerWindow);
tempfullScreenWindow.Left = screen.WorkingArea.Left;
tempfullScreenWindow.Top = screen.WorkingArea.Top;
tempfullScreenWindow.Width = screen.Bounds.Width;
tempfullScreenWindow.Height = screen.Bounds.Height;
// InvalidOperationException comes here (Specified element is already the logical child of another element. Disconnect it first.)
tempfullScreenWindow.Content = this;
tempfullScreenWindow.Owner = ownerWindow;
tempfullScreenWindow.ShowDialog();
}
else
{
if (tempfullScreenWindow != null)
{
tempfullScreenWindow.Close();
}
}
}
How can I set the usercontrol as content of the newly created window, by detaching it from owner window and when temp window is closed, re-attach the same to parent window.
Use
Make sure for a control can have only one parent at a time. Detach from first window first and then attach it to other.