ListView控件不正确的ObservableCollection更新 [英] ListView not updated correctly with ObservableCollection

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

问题描述

我目前使用的是可观察到的集合来存储我的数据对象的一个​​ListView。添加新的对象来收集工作得很好,和列表视图正确更新。然而,当我试图改变集合ListView控件将无法正常更新一个对象的属性之一。例如,我有一个观察的集合DataCollection。我试着

I'm currently using an observable collection to store my data objects for a ListView. Adding new objects to the collection works just fine, and the listView updates properly. However when I try to change one of the properties of an object in the collection the listView will not update properly. For example, I have an observable collection DataCollection. I try

_DataCollections.ElementAt(count).Status = "Active";

我执行长操作在此之前的变化,由于一个按钮preSS。列表视图不会反映更改。所以我想补充 myListView.Items.Refresh() ;.这工作,但在ListView没有得到刷新,直到button_click手段齐全,这是再没有好。
例如:

I perform this change before a long operation due to a button press. The listView will not reflect the change. So I addmyListView.Items.Refresh();. This works, however the listView does not get refreshed till button_click method is complete, which is no good by then. For example:

   button1_Click(...)
    {
      _DataCollections.ElementAt(count).Status = "Active";
      myListView.Items.Refresh();
      ExecuteLongOperation();
      _DataCollections.ElementAt(count).Status = "Finished";
      myListView.Items.Refresh();
    }

永远GOTO活动状态,它会直接进入已完成的方法完成后。
我使用也尝试过这样的调度:

The status will never goto "Active", it will go straight to "Finished" after the method completes. I also tried using a dispatcher like this:

button1_Click(...)
    {
      this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
            (NoArgDelegate)delegate { _DataCollection.ElementAt(count).Status =  "Active"; myListView.Items.Refresh(); });

      ExecuteLongOperation();
     this.Dispatcher.Invoke(System.Windows.Threading.DispatcherPriority.Background,
            (NoArgDelegate)delegate { _DataCollection.ElementAt(count).Status =  "Finished"; myListView.Items.Refresh(); });

    }

不过,这似乎并不要么正常工作。任何提示或想法将AP preciated。

However, that does not seem to work correctly either. Any tips or ideas would be appreciated.

推荐答案

要解决这个问题,我创建了一个名为VeryObservableCollection类。对于每一个对象,你添加,它挂钩对象的NotifyPropertyChanged事件来触发一个CollectionChanged事件的处理程序。对于删除的每个对象,它消除了处理程序。很简单,给你你想要什么。偏code:

To solve this I created a class called VeryObservableCollection. For each object you add, it hooks the object's NotifyPropertyChanged event to a handler that triggers a CollectionChanged event. For each object removed, it removes the handler. Very simple and will give you exactly what you want. Partial code:

public class VeryObservableCollection<T> : ObservableCollection<T>

/// <summary>
/// Override for setting item
/// </summary>
/// <param name="index">Index</param>
/// <param name="item">Item</param>
protected override void SetItem(int index, T item)
{
    try
    {
        INotifyPropertyChanged propOld = Items[index] as INotifyPropertyChanged;
        if (propOld != null)
            propOld.PropertyChanged -= new PropertyChangedEventHandler(Affecting_PropertyChanged);
    }
    catch (Exception ex)
    {
        Exception ex2 = ex.InnerException;
    }
    INotifyPropertyChanged propNew = item as INotifyPropertyChanged;
    if (propNew != null)
        propNew.PropertyChanged += new PropertyChangedEventHandler(Affecting_PropertyChanged);

    base.SetItem(index, item);
}

这篇关于ListView控件不正确的ObservableCollection更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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