【问题标题】:AvalonDock with MVVM, document close doesn't remove i tem in the DocumentsSource带有 MVVM 的 AvalonDock,文档关闭不会删除 DocumentsSource 中的 i tem
【发布时间】:2017-06-19 14:10:24
【问题描述】:

我已将 AvalonDock 的 DockingManagerDocumentsSource 绑定到我的 ViewModel 的 ObservableCollection,遵循文章 AvalonDock 2.0 with MVVM

将 ViewModel 添加到我的集合会打开正确地将其添加到新选项卡中,但是虽然我希望如果我从 UI 关闭选项卡,它会从集合中删除,但事实并非如此:集合保持其先前的计数虚拟机,与打开的选项卡不同步。

当标签页关闭时,如何从集合中删除虚拟机?这篇文章只是向 ViewModel 添加了一个 IsClosed 绑定属性,但我会发现从集合中删除它更合乎逻辑。

【问题讨论】:

  • @Massimiliano 不是真的重复,因为提到的问题是关于崩溃的,但它让我找到了 DocumentClosed 委托解决方案。非常感谢!
  • 不客气。

标签: wpf mvvm avalondock


【解决方案1】:

DocumentsSource 集合并没有真正以双向方式绑定,我必须将 DockingManager.DocumentClosed 订阅到从集合中删除 ViewModel 的委托:

private void DocumentClosed(object sender, DocumentClosedEventArgs e)
{
    // Get the VM associated with the closed document
    var documentVM = (ViewModel) e.Document.Content;
    var mainVM = (MainViewModel) DataContext;

    // Remove it from the tabs
    mainVM.Tabs.Remove(documentVM );
}

【讨论】:

  • 谢谢你,在我偶然发现你的答案之前,我已经尝试了很长时间。
  • 天哪,我什至没有注意到我有这个问题,但我很高兴能早点发现这个问题。非常感谢!
猜你喜欢
  • 2023-03-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多