WPF ListView:更改ItemsSource不会更改ListView [英] WPF ListView: Changing ItemsSource does not change ListView

查看:713
本文介绍了WPF ListView:更改ItemsSource不会更改ListView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用ListView控件来显示一些数据行.有一个后台任务,该任务接收列表内容的外部更新.新接收到的数据可能包含更少,更多或相同数量的项目,并且项目本身可能已更改.

I am using a ListView control to display some lines of data. There is a background task which receives external updates to the content of the list. The newly received data may contain less, more or the same number of items and also the items itself may have changed.

ListView.ItemsSource已绑定到OberservableCollection(_itemList),因此对_itemList的更改也应在ListView中可见.

The ListView.ItemsSource is bound to an OberservableCollection (_itemList) so that changes to _itemList should be visible also in the ListView.

_itemList = new ObservableCollection<PmemCombItem>();
_itemList.CollectionChanged += new NotifyCollectionChangedEventHandler(OnCollectionChanged);
L_PmemCombList.ItemsSource = _itemList;

为了避免刷新整个ListView,我对新检索到的列表与当前_itemList进行了简单比较,更改了不相同的项目,并在必要时添加/删除了项目.集合"newList"包含新创建的对象,因此替换_itemList中的项目将正确发送刷新"通知(我可以使用ObservableCollection`的事件处理程序OnCollectionChanged进行记录)

In order to avoid refreshing the complete ListView I do a simple comparison of the newly retrieved list with the current _itemList, change items which are not the same and add/remove items if necessary. The collection "newList" contains newly created objects, so replacing an item in _itemList is correctly sending a "Refresh" notification (which I can log by using the event handler OnCollectionChanged of the ObservableCollection`)

Action action = () =>
{
    for (int i = 0; i < newList.Count; i++)
    {
        // item exists in old list -> replace if changed
        if (i < _itemList.Count)
        {
            if (!_itemList[i].SameDataAs(newList[i]))
                _itemList[i] = newList[i];
        }
        // new list contains more items -> add items
        else
            _itemList.Add(newList[i]);
     }
     // new list contains less items -> remove items
     for (int i = _itemList.Count - 1; i >= newList.Count; i--)
         _itemList.RemoveAt(i);
 };
 Dispatcher.BeginInvoke(DispatcherPriority.Background, action);

我的问题是,如果在此循环中更改了许多项目,则ListView不会刷新,并且屏幕上的数据将保持原样……这是我不理解的.

My problem is that if many items are changed in this loop, the ListView is NOT refreshing and the data on screen stay as they are...and this I don't understand.

甚至是一个更简单的版本(交换所有元素)

Even a simpler version like this (exchanging ALL elements)

List<PmemCombItem> newList = new List<PmemCombItem>();
foreach (PmemViewItem comb in combList)
    newList.Add(new PmemCombItem(comb));

if (_itemList.Count == newList.Count)
    for (int i = 0; i < newList.Count; i++)
        _itemList[i] = newList[i];
else
{
    _itemList.Clear();
    foreach (PmemCombItem item in newList)
        _itemList.Add(item);
}

无法正常工作

对此有任何线索吗?

更新

如果我在更新所有元素后手动调用以下代码,则一切正常

If I call the following code manually after updating all elements, everything works fine

OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));

但是,这当然会导致UI更新我仍然要避免的所有内容.

But of course this causes the UI to update everything which I still want to avoid.

推荐答案

这是我要使其正常工作所要做的.

This is what I had to do to get it to work.

MyListView.ItemsSource = null;
MyListView.ItemsSource = MyDataSource;

这篇关于WPF ListView:更改ItemsSource不会更改ListView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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