WPF – AvalonDock – 结算文件
我在WPF项目中使用AvalonDock和MVVM。
当我点击“X”(选项卡的关闭按钮)时,我的文档关闭但保留在内存中。 它似乎只是隐藏了。 它不会从我的Model.Documents
集合中删除。
如果我添加DockingManager_DocumentClosing
并尝试从集合中删除我的文档,我会在Xceed.Wpf.AvalonDock.Layout.LayoutContent
的以下方法中收到Exception,因为parentAsContainer
为null。
/// /// Close the content /// /// Please note that usually the anchorable is only hidden (not closed). By default when user click the X button it only hides the content. public void Close() { var root = Root; var parentAsContainer = Parent as ILayoutContainer; parentAsContainer.RemoveChild(this); if (root != null) root.CollectGarbage(); OnClosed(); }
有没有人知道如何在AvalonDock中管理可以从我的Model.Documents
中删除的Model.Documents
,以便在我点击其Close
按钮时最终被处理掉?
供参考:这是我的AvalonDock的XAML:
<!---->
我实际上找到了一个不可接受的解决方法。 真是扭曲了。
我只是作为参考。 应该有一个干净的方法来做到这一点。
// ************************************************************************ 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); } }
我发现在LayoutDocument
或LayoutAnchorablePane
,应用这两个设置都有效: CanClose="False"
或CanFloat="False"
。
它会删除“关闭”按钮。
注册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(); }