New to Telerik UI for WPF? Download free 30-day trial

Save/load Layout with LINQ to XML

In many scenarios when creating an application that requires a more complex and flexible layout using RadDocking control as a layout layer is the way to go. One of the built-in features that enrich the control’s functionality is the ability to easily save and load the state of the entire RadDocking control’s layout. The built-in SaveLayout/LoadLayout functionality of the control uses XML in order to serialize and deserialize the state of the RadSplitContainer, RadPaneGroup, RadPane and RadDocumentPane instances in the control. XML is widely adopted and use as a way of formatting data in many contexts. Unfortunately working with XML is not a straight forward and easy task when customizing its notes and value is required. In scenarios where reordering and working with collections has been required developers turn to use the well known LINQ library. In the latest versions of .NET Framework a support for “LINQ to XML” has been provided in order to ease the described tasks.

Make sure that the System.Xml.Linq assembly is included in the project.

Create a new application and add the following declaration.

        <RowDefinition Height="Auto"/> 
        <RowDefinition Height="*"/> 
        <Button Content="Save layout to XElement"  
        <Button Content="Load layout from XElement"  
    <telerik:RadDocking x:Name="radDocking" 
                <telerik:RadPaneGroup x:Name="DocumentGroup"> 
                    <telerik:RadPane x:Name="LayoutXml" Header="Layout Xml" 
                telerik:RadDocking.FloatingSize="450, 400"> 
                        <TextBox TextWrapping="Wrap" x:Name="XmlTextBox" 
            Text="{Binding Xml, Mode=TwoWay}" /> 
                    <telerik:RadDocumentPane Header="Document 1" Title="Document 1" 
                        <TextBox x:Name="DocumentTextBox" Text="Some text" /> 
        <telerik:RadSplitContainer InitialPosition="DockedLeft"> 
                <telerik:RadPane Header="Pane Left 1" 
                    <TextBox x:Name="TextBox" Text="Some other text" /> 
                <telerik:RadPane Header="Pane Left 2" 
                    <TextBox Text="Pane Left 2" /> 
                <telerik:RadPane Header="Pane Left 3" 
                    <TextBox Text="Pane Left 3" /> 
                <telerik:RadPane Header="Pane Left 4" 
                    <TextBox Text="Pane Left 4" /> 
        <telerik:RadSplitContainer InitialPosition="DockedRight"> 
                <telerik:RadPane Header="Pane Right 1" 
                        <telerik:RadTreeViewItem Header="RootItem"> 
                            <telerik:RadTreeViewItem Header="Item1" /> 
                            <telerik:RadTreeViewItem Header="Item2"> 
                                <telerik:RadTreeViewItem Header="Item21" /> 
                                <telerik:RadTreeViewItem Header="Item22" /> 
                                <telerik:RadTreeViewItem Header="Item23"> 
                                    <telerik:RadTreeViewItem Header="Item231" /> 
                            <telerik:RadTreeViewItem Header="Item3" /> 
        <telerik:RadSplitContainer InitialPosition="DockedTop"> 
                <telerik:RadPane Header="Pane Top 1" 
                    <TextBox Text="Pane Top 1" /> 

Save layout’s XML to XElement

Example 1 shows how you can convert the generated XML from the SaveLayout functionality of the RadDocking control into valid XElement:

  • Create a new instance of a MemoryStream that will be used to temporary store the generated XML from the built-in SaveLayout functionality of the RadDocking control.

  • Use the SaveLayout method of the control with the above temporary MemoryStream object.

  • Create a field of XElement type in which the converter structure from the XML will be stored.

  • Use the static Load method of the XElement class with the temporary MemoryStream object in order to convert it to XElement

private XElement dockingLayout; 
private void OnSaveLayoutButtonClick(object sender, RoutedEventArgs e) 
    this.dockingLayout = this.GetXLayoutAsElement(); 
private XElement GetXLayoutAsElement() 
    var destinationStream = new MemoryStream(); 
    destinationStream.Seek(0, SeekOrigin.Begin); 
    return XElement.Load(destinationStream); 

Load layout’s from XElement

Example 2 shows how to load a RadDocking control's layout stored as valid XElement object:

  • Create a temporary object of MemoryStream which will be used by the LoadLayout method of the control.

  • Use the Save method of the XElement class and pass the temporary MemoryStream object as parameter.

  • Call the LoadLayout method of the control with the temporary MemoryStream object.

private void OnLoadLayoutButtonClick(object sender, RoutedEventArgs e) 
    MemoryStream sourceAsStream = new MemoryStream(); 
    sourceAsStream.Seek(0, SeekOrigin.Begin); 
In this article