Problems on persisting a dynamic layout

Sep 17, 2013 at 11:50 AM
Hello,

I am not able to load a previous saved layout. Could someone help me? Bellow my problem description and a sample project.

GOAL:
  1. I want to be able to move, dock and persist "Charts" positions in "Pages".
  2. I want to add and remove "Chart" to a selected "Page".
PROBLEM:
  1. Unable to load the persisted chart layout.
IMPLEMENTATION:
  1. My xaml contains only one LayoutAnchorablePane:
    <avalonDock:DockingManager Grid.Row="1"  x:Name="dockingManager" AnchorablesSource="{Binding SelectedPage.Charts}" 
                                       ActiveContent="{Binding SelectedPage.SelectedChart, Mode=TwoWay}" Loaded="dockingManager_Loaded_1" Unloaded="dockingManager_Unloaded_1">
        <avalonDock:DockingManager.Theme>
            <avalonDock:AeroTheme />
        </avalonDock:DockingManager.Theme>
        <avalonDock:DockingManager.LayoutUpdateStrategy>
            <local:LayoutUpdateStrategy />
        </avalonDock:DockingManager.LayoutUpdateStrategy>
        <avalonDock:LayoutRoot >
            <avalonDock:LayoutPanel Orientation="Horizontal">
                <avalonDock:LayoutAnchorablePane x:Name="layoutAnchorablePane">
    
                </avalonDock:LayoutAnchorablePane>
            </avalonDock:LayoutPanel>
        </avalonDock:LayoutRoot>
        <avalonDock:DockingManager.LayoutItemTemplate>
            <DataTemplate>
                <TextBlock Text="{Binding Title}"></TextBlock>
            </DataTemplate>
        </avalonDock:DockingManager.LayoutItemTemplate>
    </avalonDock:DockingManager>
    
    
  2. I implemented ILayoutUpdateStrategy to add LayoutAnchorable items (my Charts) to the LayoutAnchorablePane and to set LayoutAnchorable items ContentId
    public bool BeforeInsertAnchorable(LayoutRoot layout, LayoutAnchorable anchorableToShow, ILayoutContainer destinationContainer)
    {
        var pane = layout.Descendents().OfType<LayoutAnchorablePane>().FirstOrDefault();
        if (pane != null)
        {
            anchorableToShow.ContentId = ((Chart)anchorableToShow.Content).ContentId;
            pane.Children.Add(anchorableToShow);
            return true;
        }
        return false;
    }
    
  3. I persist the layout through XmlLayoutSerializer when loading the dockingManager or when changing selected Page
    private void LoadPageLayout(AvalonDock_Persistence_with_dynamic_layout.ViewModel.Page selectedPage)
    {
        if (this.dockingManager == null)
        {
            return;
        }
    
        XmlLayoutSerializer layoutSerialiser = new XmlLayoutSerializer(this.dockingManager);
    
        string layoutToRestore = selectedPage.SerializedLayout;
        if (!String.IsNullOrEmpty(layoutToRestore))
        {
            StringReader textReader = new StringReader(layoutToRestore);
            layoutSerialiser.Deserialize(textReader);
        }
    }
    
    private void SavePageLayout(AvalonDock_Persistence_with_dynamic_layout.ViewModel.Page selectedPage)
    {
        XmlLayoutSerializer layoutSerialiser = new XmlLayoutSerializer(dockingManager);
    
        StringWriter textWriter = new StringWriter();
        layoutSerialiser.Serialize(textWriter);
        selectedPage.SerializedLayout = textWriter.ToString();
    }
    
    private void ListBox_SelectionChanged_1(object sender, SelectionChangedEventArgs e)
    {
        if (e.RemovedItems.Count > 0)
        {
            var previousSelection = (AvalonDock_Persistence_with_dynamic_layout.ViewModel.Page)e.RemovedItems[0];
            SavePageLayout(previousSelection);
        }
    
        var currentSelection = (AvalonDock_Persistence_with_dynamic_layout.ViewModel.Page)e.AddedItems[0];
        LoadPageLayout(currentSelection);
    }
    
    private void dockingManager_Loaded_1(object sender, RoutedEventArgs e)
    {
        var currentSelection = ((MainViewModel)DataContext).SelectedPage;
        LoadPageLayout(currentSelection);
    }
    
  4. Attached you can find a sample project.
Sep 17, 2013 at 11:54 AM
A sample project can be found on: http://www.sendspace.com/file/07ws19