Search code examples
c#avalondock

In Avalondock, how I find a LayoutContent by ConentId


I have several panes containing content that all have their ContentIds. I want to be able to find one of the panes so I can set it to the active content. Let's call that MyContentView. In a different view, I press a button that does something like this:

LayoutContent content = FindContentById("myContent");
if(content == null)
{
    content = new MyContentView();
    content.ContentId = "myContent";
    this.MyLayoutDocumentPane.Children.Add(content);
}

this.MyDockingManager.ActiveContent = content;

I can't just hold on to that content because I will later serialize the layout, close the app, and deserialize on startup. This code will not run and I will not have that reference.

Why not just loop down the MyLayoutDocument Pane children? MyContentView can float and when that happens, it is no longer in that container.


Solution

  • You can enumerate through all existing LayoutContent items, regardless of what container they are in, as follows:

    foreach (var lc in dockingManager.Layout.Descendents().OfType<LayoutContent>())
    { /* do something */ }
    

    Descendents() is an extension method contained in the Xceed.Wpf.AvalonDock.Layout.Extensions static class, so you have to add this using:

    using Xceed.Wpf.AvalonDock.Layout;