title: WPF - AvalonDock - Closing Document
body: <p>I use AvalonDock with MVVM in a WPF project.</p>
  
  <p>When I hit the "X" (Close button of the tab) my document closes but stays in memory. It seems that it is only hidden. It is not removed from my <code>Model.Documents</code> collection.</p>
  
  <p>If I add <code>DockingManager_DocumentClosing</code> and try to remove my document from the collection, I receive an Exception in the following method of <code>Xceed.Wpf.AvalonDock.Layout.LayoutContent</code> because <code>parentAsContainer</code> is null.</p>
  
  <pre><code>/// &lt;summary&gt;
  /// Close the content
  /// &lt;/summary&gt;
  /// &lt;remarks&gt;Please note that usually the anchorable is only hidden (not closed). By default when user click the X button it only hides the content.&lt;/remarks&gt;
  public void Close()
  {
      var root = Root;
      var parentAsContainer = Parent as ILayoutContainer;
      parentAsContainer.RemoveChild(this);
      if (root != null)
          root.CollectGarbage();
      OnClosed();
  }
  </code></pre>
  
  <p>Does anybody know how I could manage document in AvalonDock that can be removed from my <code>Model.Documents</code> in order to be eventually be disposed when I hit its <code>Close</code> button?</p>
  
  <p>For reference: This is my XAML of the AvalonDock:</p>
  
  <pre><code>&lt;avalonDock:DockingManager
      x:Name="DockingManager" 
      DocumentsSource="{Binding DocumentItems}"  
      ActiveContent="{Binding ActiveMainWindowViewModel,
          Converter={StaticResource RestrictedClassConverter},
          ConverterParameter={x:Type multiSimAnalysis:MainWindowViewModel},
          Mode=TwoWay}"
      DocumentClosing="DockingManager_DocumentClosing"
      ActiveContentChanged="DockingManager_ActiveContentChanged"&gt;
  
    &lt;avalonDock:DockingManager.LayoutItemContainerStyleSelector&gt;
      &lt;pane:PanesStyleSelector&gt;
        &lt;pane:PanesStyleSelector.MainWindowViewLcStyle&gt;
          &lt;Style TargetType="{x:Type avalonDock:LayoutItem}"&gt;
            &lt;Setter Property="Title" Value="{Binding Model.Title}"/&gt;
            &lt;Setter Property="ToolTip" Value="{Binding Model.Title}"/&gt;
            &lt;Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}"/&gt;
            &lt;Setter Property="IconSource" Value="{Binding Model.IconSource}"/&gt;
            &lt;Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/&gt;
            &lt;Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/&gt;
            &lt;Setter Property="ContentId" Value="{Binding Model.ContentId}"/&gt;
          &lt;/Style&gt;
        &lt;/pane:PanesStyleSelector.MainWindowViewLcStyle&gt;
      &lt;/pane:PanesStyleSelector&gt;
    &lt;/avalonDock:DockingManager.LayoutItemContainerStyleSelector&gt;
  
    &lt;avalonDock:DockingManager.LayoutItemTemplateSelector&gt;
      &lt;multiSimAnalysis:PanesTemplateSelector&gt;
        &lt;multiSimAnalysis:PanesTemplateSelector.MainWindowLcTemplate&gt;
          &lt;DataTemplate&gt;
            &lt;multiSimAnalysis:MainWindowViewLc /&gt; 
          &lt;/DataTemplate&gt;
        &lt;/multiSimAnalysis:PanesTemplateSelector.MainWindowLcTemplate&gt;
      &lt;/multiSimAnalysis:PanesTemplateSelector&gt;
    &lt;/avalonDock:DockingManager.LayoutItemTemplateSelector&gt;
  
    &lt;avalonDock:DockingManager.Theme&gt;
      &lt;avalonDock:VS2010Theme/&gt;
    &lt;/avalonDock:DockingManager.Theme&gt;
    &lt;avalonDock:LayoutRoot&gt;
      &lt;avalonDock:LayoutPanel Orientation="Horizontal"&gt;
        &lt;avalonDock:LayoutAnchorablePane DockWidth="400"&gt;
          &lt;avalonDock:LayoutAnchorable Title="Scope(s) selection" x:Name="PanelScopeSelection" IsVisible="True"&gt;
            &lt;scopeSelection:UserControlSelectStudyScope x:Name="ToolScopeSelection"/&gt;
          &lt;/avalonDock:LayoutAnchorable&gt;
        &lt;/avalonDock:LayoutAnchorablePane&gt;
        &lt;avalonDock:LayoutDocumentPane/&gt;
        &lt;avalonDock:LayoutAnchorablePane DockWidth="150"&gt;
          &lt;avalonDock:LayoutAnchorable Title="Properties" x:Name="PanelScopePropertyGrid"&gt;
            &lt;!--&lt;multiSimAnalysis:UserControlPropertyGrid x:Name="ToolPropertyGrid"  /&gt;--&gt;
            &lt;xctk:PropertyGrid x:Name="ToolPropertyGrid" SelectedObject="{Binding ActiveObject}" /&gt;
          &lt;/avalonDock:LayoutAnchorable&gt;
        &lt;/avalonDock:LayoutAnchorablePane&gt;
      &lt;/avalonDock:LayoutPanel&gt;
    &lt;/avalonDock:LayoutRoot&gt;
  &lt;/avalonDock:DockingManager&gt;
  </code></pre>