3
Vote

[Version 2.0] Move Control between multiple DockingManagers (Reusing same control)

description

From discussion
https://avalondock.codeplex.com/discussions/445523

Closing a LayoutAnchorable doesn't seems to remove its content from the visual tree.

-=-=-=-=-=-=
I have an application with two docking managers.
First I am adding Control1 is added to the dockingManager1 . At a later time I want to move Control1 from dockingManager1 to dockingManager2 . I can't figure out how. I get the exception
Specified element is already the logical child of another element. Disconnect it first
My Code for adding initially is -
layoutAnchorable = new LayoutAnchorable() { ContentId = "Help", Title = "Help", Content = Control1, };
layoutAnchorable.AddToLayout(dockingManager1, AnchorableShowStrategy.Top);

To move later I tried -
layoutAnchorable.Close();
layoutAnchorable.Content = null;

LayoutAnchorable newLayoutAnchorable = new LayoutAnchorable() { ContentId="Help", Title = "Help", Content = Control1 };
newLayoutAnchorable.AddToLayout(dockingManager2, AnchorableShowStrategy.Top);
Whatever I do - it gives the same exception. Note: I want to use the same control - not create a new instance. What do I have to do - to have the control removed from dockingManager1 's dependency tree completely.

thanks,
V

comments

emartin wrote Jun 3, 2013 at 3:41 PM

I was on the steps of resolvig the problem.
There is a commented line in DockingManager in this method:

DockingManager.CollectLayoutItemsDeleted()

The commented line remove the anchorable content from the dockingmanager logical children. I absolutely don't know why Adospace commented this line. It was initaly checked in as is.
But it is only part of the story.
  1. You also need to get rid of the "LayoutElement._root" field, which is some kind of a cache to the "Root" dynamic property. Just call "Root" instead of refering the "_root" field.
2.
DockingManager.CollectLayoutItemsDeleted() "Dispatch" the execution of the cleanup. Removing the "dispatching" operation and simply do the cleanup at once resolve your problem, but re-add an old one. The reason why Adospace checked in this "dispatch" is to fix the issue explained in this discussion. So to avoid to re-insert an old problem to fix a new one, I was studying a way to fix this old issue without "dispatching" the execution in this method. Not complete yet.