Floating Window error at Window.GetWindow(this)

Mar 19, 2009 at 11:11 AM
Hi,

While trying to restore the saved floating Window, following error occurred

InvalidOperationException:  Cannot set Owner property to a Window that has not been shown previously.

Can you please help me fix this error? Below is the RestoreLayout method.

Thanks in advance
John

 

void RestoreLayout(XmlDocument doc)

 

{

 

if (doc.DocumentElement == null ||

 

doc.DocumentElement.Name !=

"DockingManager")

 

{

 

Debug.Assert(false, "Layout file does not have a valid structure!");

 

 

return;

 

}

 

if (doc.DocumentElement.ChildNodes.Count != 3 ||

 

(doc.DocumentElement.ChildNodes[0].Name !=

"ResizingPanel" && doc.DocumentElement.ChildNodes[0].Name != "DocumentPanePlaceHolder") ||

 

doc.DocumentElement.ChildNodes[1].Name !=

"Hidden" ||

 

doc.DocumentElement.ChildNodes[2].Name !=

"Windows")

 

{

 

Debug.Assert(false, "Layout file does not have a valid structure!");

 

 

return;

 

}

 

DockableContent[] actualContents = DockableContents;

 

 

 

//show all hidden contents

 

ShowAllHiddenContents();

 

//restore main panel

 

 

XmlElement rootElement = doc.DocumentElement.ChildNodes[0] as XmlElement;

 

 

if (rootElement.Name == "ResizingPanel")

 

{

 

this.Content = RestoreLayout(rootElement, actualContents);

 

}

 

else if (rootElement.Name == "DocumentPanePlaceHolder")

 

{

 

DocumentPaneResizingPanel existingDocumentPanel = null;

 

 

DocumentPane mainExistingDocumentPane = null;

 

RestoreDocumentPaneLayout(rootElement,

out mainExistingDocumentPane, out existingDocumentPanel);

 

 

if (existingDocumentPanel != null)

 

{

 

this.Content = existingDocumentPanel;

 

}

 

else if (mainExistingDocumentPane != null)

 

{

 

this.Content = mainExistingDocumentPane;

 

}

}

 

//restore hidden contents

 

 

foreach (XmlElement hiddenContentElement in doc.DocumentElement.ChildNodes[1].ChildNodes)

 

{

 

foreach (DockableContent hiddenContent in actualContents)

 

{

 

if (hiddenContentElement.GetAttribute("Name") == hiddenContent.Name

 

&& hiddenContent.State !=

DockableContentState.Hidden)

 

{

Hide(hiddenContent);

}

}

}

 

//restore floating windows

 

 

foreach (XmlElement flWindowElement in doc.DocumentElement.ChildNodes[2].ChildNodes)

 

{

 

bool isDockableWindow = XmlConvert.ToBoolean(flWindowElement.GetAttribute("IsDockableWindow"));

 

 

Point location = new Point(XmlConvert.ToDouble(flWindowElement.GetAttribute("Left")), XmlConvert.ToDouble(flWindowElement.GetAttribute("Top")));

 

 

Size size = new Size(XmlConvert.ToDouble(flWindowElement.GetAttribute("Width")), XmlConvert.ToDouble(flWindowElement.GetAttribute("Height")));

 

 

DockableFloatingWindow flWindow = new DockableFloatingWindow(this);

 

flWindow.Left = location.X;

flWindow.Top = location.Y;

flWindow.Width = size.Width;

flWindow.Height = size.Height;

 

//flWindow.Owner = Window.GetWindow(this);

 

flWindow.Owner =

Window.GetWindow(Application.Current.MainWindow);

 

 

 

XmlElement paneElement = flWindowElement.ChildNodes[0] as XmlElement;

 

 

FloatingDockablePane paneForFloatingWindow = new FloatingDockablePane(flWindow);

 

 

if (paneElement.HasAttribute("ResizingWidth"))

 

 

ResizingPanel.SetResizeWidth(paneForFloatingWindow, XmlConvert.ToDouble(paneElement.GetAttribute("ResizeWidth")));

 

 

if (paneElement.HasAttribute("ResizingHeight"))

 

 

ResizingPanel.SetResizeHeight(paneForFloatingWindow, XmlConvert.ToDouble(paneElement.GetAttribute("ResizeHeight")));

 

paneForFloatingWindow.Anchor = (

AnchorStyle)Enum.Parse(typeof(AnchorStyle), paneElement.GetAttribute("Anchor"));

 

 

if (paneElement.HasAttribute("ShowHeader"))

 

paneForFloatingWindow.ShowHeader = paneElement.GetAttribute(

"ShowHeader").ToLower().Equals("true");

 

 

DockableContent contentToTransfer = null;

 

 

foreach (XmlElement contentElement in paneElement.ChildNodes)

 

{

#region

Find the content to transfer

 

 

foreach (DockableContent content in actualContents)

 

{

 

if (contentElement.GetAttribute("Name") == content.Name)

 

{

contentToTransfer = content;

 

break;

 

}

}

#endregion

 

 

 

if (contentToTransfer != null)

 

{

DetachContentFromDockingManager(contentToTransfer);

paneForFloatingWindow.Items.Add(contentToTransfer);

contentToTransfer.RestoreLayout(contentElement);

}

}

flWindow.HostedPane = paneForFloatingWindow;

flWindow.IsDockableWindow = isDockableWindow;

RegisterFloatingWindow(flWindow);

flWindow.ApplyTemplate();

flWindow.Show();

}

}