This project is read-only.

[Version 1.3]RestoreLayout fails when using DocumentsSource

Jul 21, 2010 at 9:20 AM
Edited Jul 21, 2010 at 9:25 AM

Hi, Ado:

The Save/Restore layout function works fine when all DocumentContents are defined in XAML code. But when they are data binded to a collection of DocumentContent, the Restore layout function would throw an exception.

Here is how you can reproduce the problem:

XAML code:

<Window .......>
    <Grid>
        <aDock:DockingManager x:Name="dockManager" Grid.Row="1" Loaded="dockManager_Loaded">
            <aDock:ResizingPanel Orientation="Vertical">
                <aDock:ResizingPanel Orientation="Horizontal">
                    <aDock:DockablePane>
                        <aDock:DockableContent Title="left" Name="left"/>
                    </aDock:DockablePane>
                    <aDock:DocumentPane>

                    </aDock:DocumentPane>
                    <aDock:DockablePane>
                        <aDock:DockableContent Title="right" Name="right"/>
                    </aDock:DockablePane>
                </aDock:ResizingPanel>
                <aDock:DockablePane>
                    <aDock:DockableContent Title="bottom" Name="bottom"/>
                </aDock:DockablePane>
            </aDock:ResizingPanel>
        </aDock:DockingManager>
    </Grid>
</Window>

Code behind:

public partial class Window1 : Window
    {
        private ObservableCollection<DocumentContent> source;
        private int count = 1;

        public Window1()
        {
            InitializeComponent();
            source = new ObservableCollection<DocumentContent>();
            dockManager.DocumentsSource = source;
        }

        private void Window_Closing(object sender, CancelEventArgs e)
        {
            dockManager.SaveLayout("layout.txt");
        }

        private void dockManager_Loaded(object sender, RoutedEventArgs e)
        {
            for (int i = 0; i < 4; i++)
            {
                DocumentContent dc = new DocumentContent
                {
                    Title = "hello" + count,
                    Name = "hello" + count
                };
                source.Add(dc);
                count++;
            }

            if (File.Exists("layout.txt"))
            {
                dockManager.RestoreLayout("layout.txt");
            }
        }
    }

 

The exception will be thrown in the setter of DockingManager.MainDocumentPane property.

The exception will no more be thrown if I change the setter to like this:

            set
{
if (_mainDocumentPane == null)
{
_mainDocumentPane = value;

if (DocumentsSource != null)
{
foreach (object newDoc in DocumentsSource)
{
if (newDoc is DocumentContent)
{
DocumentContent doc = (DocumentContent)newDoc;
if (doc.Parent != null)
{
DocumentPane pane = doc.Parent as DocumentPane;
if (pane != null)
{
pane.Items.Remove(doc);
}
} //this is the code I added
MainDocumentPane.Items.Add(newDoc);
}
else if (newDoc is FrameworkElement) //limit objects to be at least framework elements
{
DocumentContent docContainer = new DocumentContent();
docContainer.Content = newDoc;

MainDocumentPane.Items.Add(docContainer);
}
}
}
}
else
{
_mainDocumentPane = value;
}
}

but it's just a workaround, I'm afraid this may cause other troubles.

Hope you can take a look at this and fix it, Thanks!