MVVM同步收藏 [英] MVVM Sync Collections

查看:164
本文介绍了MVVM同步收藏的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有同步模式的集合,在C#和WPF匹配模型视图对象的集合对象的标准化方法?我在寻找某种类将保持同步起来有以下两个集合假设我只有几个苹果,我可以让他们全部在内存中。

另一种方式说出来,我要确保,如果我加一个苹果的苹果收集,我想有一个AppleModelView添加到收藏AppleModelViews。我可以通过听每个集合CollectionChanged事件写我自己。这似乎是一个常见的​​场景,有人比我聪明所生产的正确的方式来做到这一点。

 公共类BasketModel
{
    公众的ObservableCollection<苹果>苹果{搞定; }
}公共类BasketModelView
{
    公众的ObservableCollection< AppleModelView> AppleModelViews {搞定; }
}


解决方案

我可能没有的究竟的了解你的需求,不过路上我曾经处理过类似的情况就是对的ObservableCollection使用CollectionChanged事件,简单地创建/按要求销毁视图模型。

 无效OnApplesCollection_CollectionChanged(对象发件人,NotifyCollectionChangedEventArgs E)
{
  //只添加/删除项目如果已填充。
  如果(!IsPopulated)
    返回;  苹果苹果;  开关(e.Action)
  {
    案例NotifyCollectionChangedAction.Add:
      苹果= e.NewItems [0]苹果;
      如果(苹果!= NULL)
        AddViewModel(资产);
      打破;
    案例NotifyCollectionChangedAction.Remove:
      苹果= e.OldItems [0]苹果;
      如果(苹果!= NULL)
        RemoveViewModel(苹果);
      打破;
  }}

有可能是当你添加/删除了很多项目在ListView一些性能问题。

扩展的ObservableCollection有一个的AddRange,RemoveRange,BinaryInsert方法和补充说,通知其他人集合正在改变事件:

我们已经解决了这个。加上扩展CollectionViewSource该临时断开时集合更改它的源工作得很好。

HTH,

丹尼斯

Is there a standardized way to sync a collection of Model objects with a collection of matching ModelView objects in C# and WPF? I'm looking for some kind of class that would keep the following two collections synced up assuming I only have a few apples and I can keep them all in memory.

Another way to say it, I want to make sure if I add an Apple to the Apples collection I would like to have an AppleModelView added to the AppleModelViews collection. I could write my own by listening to each collections' CollectionChanged event. This seems like a common scenario that someone smarter than me has defined "the right way" to do it.

public class BasketModel
{
    public ObservableCollection<Apple> Apples { get; }
}

public class BasketModelView
{
    public ObservableCollection<AppleModelView> AppleModelViews { get; }
}

解决方案

I may not exactly understand your requirements however the way I have handled a similar situation is to use CollectionChanged event on the ObservableCollection and simply create/destroy the view models as required.

void OnApplesCollection_CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{    
  // Only add/remove items if already populated. 
  if (!IsPopulated)
    return;

  Apple apple;

  switch (e.Action)
  {
    case NotifyCollectionChangedAction.Add:
      apple = e.NewItems[0] as Apple;
      if (apple != null)
        AddViewModel(asset);
      break;
    case NotifyCollectionChangedAction.Remove:
      apple = e.OldItems[0] as Apple;
      if (apple != null)
        RemoveViewModel(apple);
      break;
  }

}

There can be some performance issues when you add/remove a lot of items in a ListView.

We have solved this by: Extending the ObservableCollection to have an AddRange, RemoveRange, BinaryInsert methods and adding events that notify others the collection is being changed. Together with an extended CollectionViewSource that temporary disconnects the source when the collection is changed it works nicely.

HTH,

Dennis

这篇关于MVVM同步收藏的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆