【问题标题】:Removing Item from BindableCollection of ShellViewModel with ChildViewModel?使用 ChildViewModel 从 ShellViewModel 的 BindableCollection 中删除项目?
【发布时间】:2012-05-11 14:15:46
【问题描述】:

我有一个带有 BindableCollection 的 ShellViewModel,它在 ItemControl 的帮助下绑定到了一个 Canvas。

借助 ShellViewModel 的按钮,我将派生自 Screen 的 ChildViewModel 添加到此 bindableCollection。

我在 ChildViewModel 中有一个关闭按钮,单击此按钮时,我想从 ShellViewModel 的 BindableCollection 中删除项目,

请帮忙。

【问题讨论】:

  • 我可能弄错了,但我认为从 ShellViewModel 上的 Conductor.Collection.OneActive 继承会更好。

标签: c# wpf mvvm caliburn.micro


【解决方案1】:

如果您想维护现有的实现,您可以随时创建一个事件并使用 EventAggregator。您的 ChildViewModel 需要发布事件,而 ShellViewModel 需要实现 IHandle<ChildViewModelMessage> 接口。作为该实现的一部分,它将能够从 BindableCollection 中删除 ChildViewModel。一般来说,它看起来像这样:

public class ChildViewModelMessage {
    // Implementation here
}

public class ShellViewModel : IHandle<ChildViewModelMessage> {
    ...
    public void Handle(ChildViewModelMessage message) {
        // Handle here
    }
}

public class ChildViewModel {
    ...
    public IEventAggregator Events { get; set; }
    protected void HandleClose() {
        this.Events.Publish(new ChildViewModelMessage());
    }

【讨论】:

    【解决方案2】:

    如果您需要在不同的视图模型之间进行交流,您应该使用信使。这是 mvvm light 工具包中的一个示例:http://blog.galasoft.ch/archive/2009/09/27/mvvm-light-toolkit-messenger-v2-beta.aspx

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-10-06
      • 2017-07-31
      • 2019-02-02
      • 2014-12-07
      • 1970-01-01
      • 2018-12-02
      • 2021-10-19
      • 2012-02-01
      相关资源
      最近更新 更多