AvalonDock 2.0 getting started guide PART 2

In the part of the guide will se how to use AvalonDock common features. Starting from a simple layout, we'll see how to serialize/deserialize the layout and how to interact wtih it by code.

        <avalonDock:DockingManager x:Name="dockingManager">
            <avalonDock:LayoutRoot>
                <avalonDock:LayoutPanel Orientation="Horizontal">
                    <avalonDock:LayoutAnchorablePane DockWidth="100">
                        <avalonDock:LayoutAnchorable Title="Tool 1" IconSource="/AvalonDock2Tutorial;component/Images/alarm-clock-blue.png">
                            <TextBox/>
                        </avalonDock:LayoutAnchorable>
                    </avalonDock:LayoutAnchorablePane>
                    <avalonDock:LayoutDocumentPane>
                        <avalonDock:LayoutDocument Title="Doc1">
                            <TextBox/>
                        </avalonDock:LayoutDocument>
                    </avalonDock:LayoutDocumentPane>
                </avalonDock:LayoutPanel>
                <avalonDock:LayoutRoot.BottomSide>
                    <avalonDock:LayoutAnchorSide>
                        <avalonDock:LayoutAnchorGroup>
                            <avalonDock:LayoutAnchorable Title="Autohidden Content" IconSource="/AvalonDock2Tutorial;component/Images/address-book-open.png">
                                <TextBox/>
                            </avalonDock:LayoutAnchorable>
                        </avalonDock:LayoutAnchorGroup>
                    </avalonDock:LayoutAnchorSide>
                </avalonDock:LayoutRoot.BottomSide>
            </avalonDock:LayoutRoot>
        </avalonDock:DockingManager>

To save the layout we need the XmlLayoutSerializer class and call its Serialize method:
            var serializer = new XmlLayoutSerializer(dockingManager);
            using (var stream = new StreamWriter(fileName))
                serializer.Serialize(stream);

and the Deserialize method to load a layout from a file:
            var serializer = new XmlLayoutSerializer(dockingManager);
            using (var stream = new StreamReader(fileName))
                serializer.Deserialize(stream);

If you put this code inside the application and run it you will shortly discover that even if the layout is correctly saved and loaded it doesn't restore controls inside the LayoutContent (in this case all textboxes). The problem is that AvalonDock when saving a layout stores only layout information. If you open the saved file you should see something like the following:


Last edited Sep 3, 2012 at 1:04 PM by adospace, version 2

Comments

NickStrupat Jan 29, 2013 at 1:46 PM 
For more info, my question on stackoverflow is at http://stackoverflow.com/questions/14585556/how-do-i-automatically-load-a-layout-into-my-avalondock-instance

NickStrupat Jan 29, 2013 at 1:25 PM 
How do we bind the layout of a DockingManager to a property on a view-model? My goal is to load a layout when our "project file" is opened, but I can't find an event or property that loads the layout properly. I've tried DataContextChanged but I think it fires too early because the layout loads with the documents in the hidden section and duplicate documents in the visible sections. Thanks!