带列表的OnPropertyChanged [英] OnPropertyChanged with a List

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

问题描述

我在视图中有一个数据网格,该数据网格绑定到视图模型中的列表.我需要从列表中检索很多数据,因此我想将其分解为许多小的检索,而不是一个大的检索.

I have a data grid in a view that is bound to a List in a viewmodel. I have a lot of data to retrieve for the list, so I want to break it up into many small retrievals instead of one big one.

我希望在后台线程上发生这种情况,并在每个检索到的批处理结束时进行UI更新(绑定新数据的网格).

I want this to happen on a background thread with the UI updating (the grid binding the new data) at the end of each batch retrieved.

在每次检索的最后,我在私有支持者上执行List.AddRange(),然后引发OnPropertyChanged事件,该事件传递网格绑定到的公共属性的名称.

At the end of each retrieval, I am doing a List.AddRange() on the private backer, then raising the OnPropertyChanged event passing the name of the public property that the grid is bound to.

最初,我尝试了6次迭代,每次均检索100个项目.在后台运行时,UI会在前100个之后更新,但不会更新后500个(即使数据已成功添加到viewmodel的基础列表中).

Initially I tried this with 6 iterations that retrieve 100 items each. When running in the background, the UI would update after the first 100, but then not update the last 500 (even though the data was successfully added to the underlying list in the viewmodel).

考虑到编组UI线程存在一些问题,我同步运行它,期望它可以按预期运行(尽管在每次检索过程中都阻止了UI)或在所有检索过程中都阻止了UI-但无论哪种情况,最后更新为显示600个项目.但是,它最终的效果与我在后台运行时的效果相同-仅更新前100个,而不更新其余的.

Thinking that I had some issues with marshalling to the UI thread, I ran it sychroneously, expecting it to either work as expected (albeit blocking the UI during each retrieval) or block the UI during all the retrievals - but in either case, updating at the end to show 600 items. However, it ends up doing the same thing as when I run it in the background - only updates the first 100 and not the rest.

下面是我在两次尝试中都使用的方法,上半部分是注释掉的背景版本.

Below is the method I am using with both attempts, the top half being the background version commented out.

我在做什么错了?

public void StartDataStream()
{
    //Task<List<Car>> task = _taskFactory.StartNew(this._retrieveData);

    //task.ContinueWith(t =>
    //{
    //    if (this._cars == null) this._cars = new List<Car>();

    //    this._cars.AddRange(t.Result);
    //    base.OnPropertyChanged("Cars");

    //    this.iterations += 1;
    //    if (iterations < 6) StartDataStream();
    //});

    if (this._cars == null) this._cars = new List<Car>();

    this._cars.AddRange(this.GetCarList(eq,s,e));
    base.OnPropertyChanged("Cars");

    this.iterations += 1;

    if (iterations < 6) StartDataStream();
}

推荐答案

您是否尝试过使用ObservableCollection<T>而不是List<T>

Have you tried using an ObservableCollection<T> rather than a List<T>

我假设您拥有一个与...相似的名为汽车"的公共财产.

I assume you have a public property called Cars similar to...

public List<Car> Cars{

   get { return this._cars;}
   set
   {
      this._cars = value;
      base.OnPropertyChanged("Cars");
   }

}

如果不是这样的话,实际上将不会做任何事情... base.OnPropertyChanged("Cars");

If not this will not actually do anything...base.OnPropertyChanged("Cars");

ObservableCollection的扩展方法AddRange

public static class Extensions
{
    public static void AddRange(this ObservableCollection obj, List<T> items)
    {
        foreach (var item in items)
          obj.Add(item);
    }
}

这篇关于带列表的OnPropertyChanged的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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