我目前正在使用一个可观察的集合来存储我的ListView的数据对象。将新对象添加到集合工作得很好,并且listView正确更新。但是,当我尝试更改集合中某个对象的某个属性时,listView将无法正确更新。例如,我有一个可观察的集合DataCollection。我尝试ListView未正确更新与ObservableCollection
_DataCollections.ElementAt(count).Status = "Active";
由于按钮按下,我在长操作之前执行此更改。 listView不会反映更改。所以我加myListView.Items.Refresh()
;。这是有效的,但是listView不会被刷新,直到button_click方法完成为止,这是不好的。 例如:
button1_Click(...)
{
_DataCollections.ElementAt(count).Status = "Active";
myListView.Items.Refresh();
ExecuteLongOperation();
_DataCollections.ElementAt(count).Status = "Finished";
myListView.Items.Refresh();
}
状态将永远不会GOTO“激活”,这将直接进入“完成”的方法完成后。 我还使用调度这样的尝试:
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(); });
}
然而,这似乎并没有任何正常工作。任何提示或想法,将不胜感激。
我需要很长时间才能找到Affecting_PropertyChanged方法的正确实现..你只需要一行:'MyBase.OnCollectionChanged(New NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset))'并且我推荐覆盖ObservableCollection的InsertItem和RemoveItem,因为SetItem didn不为我工作。 –
@Felix,是的,还有更多。类实现超过800行。我确实说过我已经发布了部分代码,主要是为了给出一般的想法。 –
我写了这个为其他人找到这篇文章,不知道如何实现这些代码行。这不是一个更正,只是延长这个工作。 :)但为什么你的班级有800行的长度?我真的很高兴看到你的全班实施。 –