我在 WPF 项目中使用 AvalonDock 和 MVVM。
当我点击“X”(选项卡的关闭按钮)时,我的文档将关闭但仍保留在内存中。似乎只是隐藏起来了。它没有从我的Model.Documents
收藏中删除。
如果我添加DockingManager_DocumentClosing
并尝试从集合中删除我的文档,我会在以下方法中收到一个异常,Xceed.Wpf.AvalonDock.Layout.LayoutContent
因为parentAsContainer
它为 null。
/// <summary>
/// Close the content
/// </summary>
/// <remarks>Please note that usually the anchorable is only hidden (not closed). By default when user click the X button it only hides the content.</remarks>
public void Close()
{
var root = Root;
var parentAsContainer = Parent as ILayoutContainer;
parentAsContainer.RemoveChild(this);
if (root != null)
root.CollectGarbage();
OnClosed();
}
有谁知道我如何在 AvalonDock 中管理可以从我的文档中删除的文档,以便在我点击它的按钮Model.Documents
时最终被处理?Close
供参考:这是我的 AvalonDock 的 XAML:
<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">
<avalonDock:DockingManager.LayoutItemContainerStyleSelector>
<pane:PanesStyleSelector>
<pane:PanesStyleSelector.MainWindowViewLcStyle>
<Style TargetType="{x:Type avalonDock:LayoutItem}">
<Setter Property="Title" Value="{Binding Model.Title}"/>
<Setter Property="ToolTip" Value="{Binding Model.Title}"/>
<Setter Property="CloseCommand" Value="{Binding Model.CloseCommand}"/>
<Setter Property="IconSource" Value="{Binding Model.IconSource}"/>
<Setter Property="IsSelected" Value="{Binding Model.IsSelected, Mode=TwoWay}"/>
<Setter Property="IsActive" Value="{Binding Model.IsActive, Mode=TwoWay}"/>
<Setter Property="ContentId" Value="{Binding Model.ContentId}"/>
</Style>
</pane:PanesStyleSelector.MainWindowViewLcStyle>
</pane:PanesStyleSelector>
</avalonDock:DockingManager.LayoutItemContainerStyleSelector>
<avalonDock:DockingManager.LayoutItemTemplateSelector>
<multiSimAnalysis:PanesTemplateSelector>
<multiSimAnalysis:PanesTemplateSelector.MainWindowLcTemplate>
<DataTemplate>
<multiSimAnalysis:MainWindowViewLc />
</DataTemplate>
</multiSimAnalysis:PanesTemplateSelector.MainWindowLcTemplate>
</multiSimAnalysis:PanesTemplateSelector>
</avalonDock:DockingManager.LayoutItemTemplateSelector>
<avalonDock:DockingManager.Theme>
<avalonDock:VS2010Theme/>
</avalonDock:DockingManager.Theme>
<avalonDock:LayoutRoot>
<avalonDock:LayoutPanel Orientation="Horizontal">
<avalonDock:LayoutAnchorablePane DockWidth="400">
<avalonDock:LayoutAnchorable Title="Scope(s) selection" x:Name="PanelScopeSelection" IsVisible="True">
<scopeSelection:UserControlSelectStudyScope x:Name="ToolScopeSelection"/>
</avalonDock:LayoutAnchorable>
</avalonDock:LayoutAnchorablePane>
<avalonDock:LayoutDocumentPane/>
<avalonDock:LayoutAnchorablePane DockWidth="150">
<avalonDock:LayoutAnchorable Title="Properties" x:Name="PanelScopePropertyGrid">
<!--<multiSimAnalysis:UserControlPropertyGrid x:Name="ToolPropertyGrid" />-->
<xctk:PropertyGrid x:Name="ToolPropertyGrid" SelectedObject="{Binding ActiveObject}" />
</avalonDock:LayoutAnchorable>
</avalonDock:LayoutAnchorablePane>
</avalonDock:LayoutPanel>
</avalonDock:LayoutRoot>
</avalonDock:DockingManager>