This project is read-only.

[Version 2.0] Bug? Deserializing floating window results in two views

Dec 4, 2012 at 12:42 AM

I've got a weird problem here and I'm not sure if it's a bug or a problem in my code.

i'm using the following code to serialize the layout into my app settings:

using (var memoryStream = new MemoryStream()) {
	new XmlLayoutSerializer(MainWindow.sharedDockingManager).Serialize(memoryStream);
	memoryStream.Seek(0, SeekOrigin.Begin);
	var layout = new StreamReader(memoryStream).ReadToEnd();
	Settings.Default.AvalonDockLayout = layout;

And this to deserialize:

string layout = Settings.Default.AvalonDockLayout;
var bytes = Encoding.Default.GetBytes(layout);
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(bytes, 0, bytes.Length);
memoryStream.Seek(0, SeekOrigin.Begin);
XmlLayoutSerializer layoutSerializer = new XmlLayoutSerializer(MainWindow.sharedDockingManager);
layoutSerializer.LayoutSerializationCallback += (s, e) =>
	if (!string.IsNullOrWhiteSpace(e.Model.ContentId))
		e.Content =

Where the callback uses this helper method to get or create a viewModel:

case "CommoditySearchToolWindow":
    var commoditySearchToolWindowViewModel =
    if (commoditySearchToolWindowViewModel != null)
        return commoditySearchToolWindowViewModel;

    commoditySearchToolWindowViewModel = new CommoditySearchToolWindowViewModel();
    Workspace.This.AvalonDockAdapter.AddAnchorable(commoditySearchToolWindowViewModel, false);
    return commoditySearchToolWindowViewModel;

This works perfectly for docked AnchorablePanes, but if a pane is floating when it's serialized, upon deserialization it produces a window containing my view and hooked up to the viewmodel correctly, and another directly behind it in the z-index (the same size) with the same properties (title, etc) but no view. Clicking the X on one of the windows will hide both, and reopening one will open both again. Docking either of the floating windows to the main window will cause both to dissapear and the correct one (with the view) to dock, and then floating the now docked pane results in correction of the problem with only the one window floating.

Using version 2.0.1737.0 built from source.

Dec 4, 2012 at 8:54 AM

much probably you're adding two times the same contentid (one to the anchorables collection and one returned to the callback handler). Said that I'm sure there is also a bug somewhere that shows the two floating windows.

It would be really useful if you could wrap a really small sample project that shows the problem,


Dec 4, 2012 at 5:12 PM

Hi Ado,

Yes I think you're correct about it being added twice, but only one goes into the collection;

When the LayoutSerializationCallback fires, calling OpenLayoutItemByContentId, that checks if the anchorable already exists in the collection, if so it simply returns the existing one, otherwise it adds a new one to the collection and returns that.

Is it possible that the layout serializer is creating a window for the returned viewmodel instance but no view  but also creating a window+view for the viewmodel in the collection?

I'll happily throw something together using my working code and send it to you in the next few days.


Current workaround for this is to dock everything that's floating before serialization (this is ugly code I know):

foreach (LayoutFloatingWindow floatingWindow in dockingManager.Layout.FloatingWindows.ToList())
    foreach (LayoutAnchorablePaneGroup anchorablePaneGroup in floatingWindow.Children.ToList())
        foreach (LayoutAnchorablePane anchorablePane in anchorablePaneGroup.Children.ToList())
            foreach (LayoutAnchorable anchorable in anchorablePane.Children.ToList())



Dec 13, 2012 at 5:36 PM


I've fixed this bug in latest build, thanks,


Dec 13, 2012 at 5:58 PM
Thanks! Appreciate the quick fix :)

Tim Hoskins
Jan 17, 2013 at 2:47 PM

Sorry to bump into this, i ran into the same issue. Where can i get the latest build? (on the main page there are no downloads after Dec. 4)