我在一个WPF项目中使用AvalonDock和MVVM。
当我点击"X“(选项卡的关闭按钮)时,我的文档将关闭,但会留在内存中。看起来它只是隐藏了起来。它不会从我的Model.Documents集合中删除。
如果我添加DockingManager_DocumentClosing并尝试从集合中移除文档,则在Xceed.Wpf.AvalonDock.Layout.LayoutContent的以下方法中收到异常,因为parentAsContainer为空。
/// <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>发布于 2013-08-21 22:49:41
实际上,我发现了一个不可接受的变通方法。它真的很扭曲。
我只给你一个参考。应该有一种干净的方法来做这件事。
// ************************************************************************
private void DockingManager_DocumentClosing(object sender, Xceed.Wpf.AvalonDock.DocumentClosingEventArgs e)
{
e.Document.CanClose = false;
DocumentModel documentModel = e.Document.Content as DocumentModel;
if (documentModel != null)
{
Dispatcher.BeginInvoke(new Action(() => this.Model.DocumentItems.Remove(documentModel)), DispatcherPriority.Background);
}
}发布于 2016-10-26 00:23:54
我发现在LayoutDocument或LayoutAnchorablePane上,应用这两个设置都有效:CanClose="False"或CanFloat="False"。
它会删除Close按钮。
<avalonDock:LayoutDocument Title="Board"
ContentId="Board"
CanClose="False"
CanFloat="False">
</avalonDock:LayoutDocument>发布于 2014-03-07 02:15:15
注册IsVisibleChanged。
void layoutFPR_Hidden(object sender, EventArgs e)
{
LayoutAnchorable window = (LayoutAnchorable)sender;
YourClass content = window.Content as YourClass;
// Close the object
content = null;
((LayoutAnchorable)sender).Close();
}https://stackoverflow.com/questions/18359818
复制相似问题