与其他的ObservableCollection更换整套的ObservableCollection [英] Replace Entire ObservableCollection with another ObservableCollection

查看:275
本文介绍了与其他的ObservableCollection更换整套的ObservableCollection的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 公共类阿尔法
{
    公众的ObservableCollection<β>项目{搞定;组; }    公共阿尔法()
    {
        项目=新的ObservableCollection<β>();
    }    公共无效DoSomething的()
    {
        项目= GetNewItems(); //每当我做到这一点,项目得到了新的referene,
                                //所以每一个WPF结合(例如数据网格)被打破
    }    公众的ObservableCollection<β> GetNewItems()
    {
         VAR RET =新的ObservableCollection<β>();
         //一些逻辑用于获取地方一些项目,并填充RET
         返回RET;
    }
}

我怎么能代替项目 GetNewItems)的返回值(没有的全部内容:


  1. 突破绑定。


  2. 有通过项目环和一个复制它们一个到另一个集合?



解决方案

您有一些选择:


  1. 实现INotifyPropertyChanged,以便您可以通知项目的值已更改UI。这个没有利用的事实INotifyCollectionChanged上的ObservableCollection实现。它会工作,但它违背了摆在首位使用一个ObservableCollection的目的。这是不推荐,但它的工作原理。

  2. 使用的ObservableCollection的添加/删除/修改/更新的方法来修改它与调度相结合。

    • 注:没有分派器,你会得到一个NotSupportedException异常原因的 Col​​lectionViews不支持从一个线程从调度线程的不同其SourceCollection变化


  3. 使用的ObservableCollection的添加/删除/修改/更新的方法与 BindingOperations.EnableCollectionSynchronization 来修改它结合。 推荐

    • 注意:这是只适用于.NET 4.5

    • 这是使用Dispatcher同时避免NotSupportedException异常的替代品。



号2和3,对于你的问题,转化为清除现有项目(清除()),然后添加(Add()方法),通过任何你想要的方法返回的项目 - 见#3的例子。他们关键的是,添加的清算和所有必须与调度员(2)或通过调用来完成 BindingOperations.EnableCollectionSynchronization
祝你好运!

参考:<一href=\"http://stackoverflow.com/questions/18579520/remove-from-observable-collection-in-viewmodel-does-not-update-view-but-updates\">Reed •科普塞答案 - 计算器

public class Alpha
{
    public ObservableCollection<Beta> Items { get; set; }

    public Alpha()
    {
        Items = new ObservableCollection<Beta>();
    }

    public void DoSomething()
    {
        Items = GetNewItems();  // whenever I do this, Items gets a new referene, 
                                // so every WPF binding (e.g. datagrids) are broken
    }

    public ObservableCollection<Beta> GetNewItems()
    {
         var ret = new ObservableCollection<Beta>();
         // some logic for getting some items from somewhere, and populating ret
         return ret;
    }
}

How can I replace the whole content of Items with the return value of GetNewItems() without:

  1. Breaking the bindings.

  2. Having to loop through the items and copy them one by one to the other collection ?

解决方案

You have some options:

  1. Implement INotifyPropertyChanged so you can inform the UI that the value of Items has changed. This does not utilize the fact that INotifyCollectionChanged is implemented on ObservableCollection. It will work, but it defeats the purpose of using an ObservableCollection in the first place. This is not recommended but it works.
  2. Use the ObservableCollection's Add/Remove/Modify/Update methods to modify it in conjunction with the Dispatcher.
    • Note: without the Dispatcher, you will get a NotSupportedException because CollectionViews do not support changes to their SourceCollection from a thread different from the Dispatcher thread.
  3. Use the ObservableCollection's Add/Remove/Modify/Update methods to modify it in conjunction with BindingOperations.EnableCollectionSynchronization. Recommended
    • Note: This is only available in .NET 4.5.
    • This is an alternative to using the Dispatcher while avoiding the NotSupportedException.
    • Example

Numbers 2 and 3, with respect to your question, translate to clearing the existing items (Clear()) and then adding (Add()) the items returned by whatever method you want - see the example for #3. They key is that the clearing and all of the adding must be done with Dispatcher (2) or by calling BindingOperations.EnableCollectionSynchronization. Good luck!

Reference: Reed Copsey Answer - StackOverflow

这篇关于与其他的ObservableCollection更换整套的ObservableCollection的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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