title: How to remove LogicalChildren from DockManager after closing tab?
body: <p>I've been using the <code>DockManager</code> with <code>LayoutRoot</code>, <code>LayoutAnchorablePane</code> and <code>LayoutDocumentPane</code>.</p>
  
  <pre><code>&lt;ad:DockingManager x:Name="dockManager" &gt;
    &lt;adLayout:LayoutRoot&gt;
      &lt;adLayout:LayoutPanel x:Name="myLayoutPanel" Orientation="Horizontal"&gt;
        &lt;adLayout:LayoutAnchorablePane x:Name="myLayoutAnchorablePane" DockWidth="400"/&gt;
        &lt;adLayout:LayoutDocumentPane x:Name="myDocumentPane" ChildrenCollectionChanged="myDocumentPane_ChildrenCollectionChanged"/&gt;
      &lt;/adLayout:LayoutPanel&gt;
    &lt;/adLayout:LayoutRoot&gt;
  &lt;/ad:DockingManager&gt;
  </code></pre>
  
  <p>However, one problem I've encounted is that in <code>DockManager.LogicalChildren</code>, the <code>ContentPresenter</code> and my <code>UserControl</code> that went into the <code>LayoutDocument</code> never gets removed when I close the window and keeps building up more and more <code>LogicalChildren</code> until it starts to slow down the application.</p>
  
  <p>How do I, when I detect <code>ChildrenCollectionChanged</code>, remove the <code>ContentPresenter</code> and <code>UserControl</code> that were associated with that <code>LayoutDocument</code>? </p>
  
  <p>Edit 1: Okay, so <code>LogicalChildren</code> is <code>System.Linq.Enumerable.WhereSelectListIterator&lt;System.WeakReference,object&gt;</code>, so I won't be able to remove anything from that list (also it only has a <code>get</code>, and no <code>set</code>).</p>
  
  <p>The <code>LayoutDocumentPane.RemoveChild()</code> method doesn't do anything to the <code>DockingManager.LogicalChildren</code>, so I can't figure out where the <code>LogicalChildren</code> is pulling the iteration date from.</p>
  
  <p>Edit 2: So, I tried adding an event to the <code>DocumentClosing</code> event handler for <code>DockManager</code> and it still doesn't seem to remove the unused <code>LogicalChildren</code> from DockManager.</p>
  
  <pre><code>void dockManager_DocumentClosing(object sender, Xceed.Wpf.AvalonDock.DocumentClosingEventArgs e) {
      UserControl uc = e.Document.Content as UserControl;
      e.Cancel = true;
      e.Document.IsActive = false;
      if(uc != null) {
          var u = myDocumentPane.Children.Where(a =&gt; a.Content.Equals(uc)).FirstOrDefault();
          u.IsActive = false;
          u.Close();
          myDocumentPane.Children.Remove(u);
          myDocumentPane.RemoveChild(u);
  
          var oldLogicalParentPaneControl = 
              LogicalTreeHelper.GetParent(u.Content as UIElement) as Xceed.Wpf.AvalonDock.DockingManager;
          oldLogicalParentPaneControl.Layout.RemoveChild(u);
          oldLogicalParentPaneControl.Layout.CollectGarbage();
          dockManager.UpdateLayout();
      }
  }
  </code></pre>
  
  <p>Edit 3: After looking at what remains in <code>LayoutDocumentPane</code> after the <code>DocumentClosed</code>  without any modification (instead of <code>DocumentClosing</code>), it seems that the User Control is removed from <code>LayoutDocumentPane</code>, but not from <code>LogicalChildren</code> still.</p>