Search code examples
wpfavalondock

AvalonDock: How can I add a document alongside another using ILayoutUpdateStrategy


I have implemented ILayoutupdateStrategy using examples here but so far, each new LayoutDocument is always placed in the same LayoutDocumentPane. I would like to add the new LayoutDocument alongside an existing Document automatically.

I've tried creating a new LayoutDocumentPaneGroup and moving the existing LayoutDocumentPane, plus the new content into that new group, but when I then try to add the group into the layout structure the methods never return and the application hangs.

LayoutDocumentPaneGroup newGroup = { a LayoutDocumentPaneGroup containing both the original sibling LayoutDocumentPane and the new content LayoutDocumentPane }
LayoutDocumentPaneGroup parent = { the parent group of the sibling document }

I've tried things like:

parent.RemoveChild(sibling);
parent.Children.Add( newGroup );

and

parent.Children.ReplaceChild( sibling, newGroup );

but they all seem to hang on those methods.

I expect I'm approaching this in the wrong way completely so any pointers would be very welcome.


Solution

  • So... it turns out the problem was that you must defer adding children to the new document group being added until after it's been placed into the layout tree.

    1. Create a new LayoutDocumentPaneGroup to contain the existing LayoutDocumentPane, and the new LayoutDocument
    2. Create a new LayoutDocumentPane to encapsulate the LayoutDocument
    3. Get a reference to the sibling LayoutDocumentPane's parent (which should be a LayoutDocumentPaneGroup)
    4. Replace the sibling LayoutDocumentPane with the new PaneGroup Add the sibling, and the new layout doc to the new Group

    Example code

        private void DockDocumentToBottom( LayoutDocumentPane sibling, LayoutDocument tobeDocked )
        {
            LayoutDocumentPaneGroup newGroup = new LayoutDocumentPaneGroup();
            newGroup.Orientation = System.Windows.Controls.Orientation.Vertical;
            LayoutDocumentPane newDocPane = new LayoutDocumentPane(tobeDocked);
    
            LayoutDocumentPaneGroup siblingParent = sibling.Parent as LayoutDocumentPaneGroup;
            if (siblingParent != null )
            {
                siblingParent.ReplaceChild(sibling, newGroup);
                newGroup.Children.Add(sibling);
                newGroup.Children.Add(newDocPane);
            }
            else
            {
                Debug.WriteLine("LayoutInitialiser: Failed to attach to a document pane group!");
            }
        }
    

    I hope this helps someone else!