[英]Prolems with ObservableCollection CollectionChanged event
I have an ObservableCollection _cableList
containing objects of type CableViewModel
. 我有一个
ObservableCollection _cableList
其中包含CableViewModel
类型的CableViewModel
。 The CableViewModel
objects have a boolean property IsInDB
that should be set if another property Type
is listed i another collection. CableViewModel
对象具有布尔属性IsInDB
,如果在另一个集合中列出了另一个属性Type
,则应设置该属性。 I listen to the PropertyChanged
event in order to update the IsInDB
property. 我听
PropertyChanged
事件以更新IsInDB
属性。 This works as intended but I also need to update the IsInDB
property for items that are added to the collection. 这可以按预期工作,但是我还需要为添加到集合中的项目更新
IsInDB
属性。 I attempt to solve this with the following code: 我尝试使用以下代码解决此问题:
public MainViewModel()
{
_cableList = new ObservableCollection<CableViewModel>();
_cableList.CollectionChanged += _cableListChanged;
}
private void _cableListChanged(object sender, NotifyCollectionChangedEventArgs e)
{
if(e.OldItems != null)
{
foreach (INotifyPropertyChanged item in e.OldItems)
item.PropertyChanged -= cablePropertyChanged;
}
if(e.NewItems != null)
{
foreach (INotifyPropertyChanged item in e.NewItems)
{
item.PropertyChanged += cablePropertyChanged;
// This causes an exception
MessageBox.Show("Collection changed - new Item");
var x = (CableViewModel)item;
x.IsInDB = IsInCableDB(x.Type);
}
}
}
The code causes an System.InvalidOperationException
that says something like "An ItemsControl
is inconsistent with its object source" or at least that´s my attempt to translate from the Swedish error message. 该代码导致
System.InvalidOperationException
类似“ ItemsControl
与它的对象源不一致”之类的信息,或者至少是我试图从瑞典语错误消息中进行翻译。
What is wrong with my code? 我的代码有什么问题? Is there a another way to solve the task?
还有另一种解决任务的方法吗?
EDIT 编辑
I did not show the complete code in my original post as I didn't think it was related to the exception. 我没有在原始帖子中显示完整的代码,因为我认为它与异常无关。 But I was wrong.
但是我错了。
In my original post I did not show or mention that I showed a MessageBox
for debugging purposes. 在我的原始帖子中,我没有显示或提及我显示了用于调试目的的
MessageBox
。 It was shown each time a new object was added to the collection. 每次向集合添加新对象时都会显示该对象。 The code above is now edited so that
MessageBox.Show
line is included. 现在,上面的代码已被编辑,因此包括
MessageBox.Show
行。 After posting the original message here I removed the MessageBox
from the code and as a result the exception disappeared. 在这里发布原始消息后,我从代码中删除了
MessageBox
,结果异常消失了。 Did the MessageBox
cause the UI to get out of sync with the collection? MessageBox
是否导致UI与集合不同步? (The ObservableCollection
is bound to a DataGrid
) (
ObservableCollection
绑定到DataGrid
)
In that case I would suggest using ReactiveList and use observables instead of events - it makes lots of threading problems go away. 在那种情况下,我建议使用ReactiveList并使用observables代替事件-这样可以避免很多线程问题。
On the plus side, you can then do something like this: 从好的方面来说,您可以执行以下操作:
_cableList.ItemChanged.Where(x => x.PropertyName == "Cable").Select(x => x.Sender).Subscribe(x => {
//do something with the object
});
_cableList.ItemsAdded.Subscribe(x => x.IsInDB = IsInCableDB(x.Type);
Ehh... my mistake. 嗯...我的错。 When authoring the code I added messageboxes to learn what was going on.
在编写代码时,我添加了消息框以了解发生了什么。 Each time the
CollectionChanged
was triggered a messagebox was shown. 每次触发
CollectionChanged
时,都会显示一个消息框。 When I removed the messageboxes the exceptions disappeared. 当我删除消息框时,异常消失了。 Did the messageboxes cause the UI get out of sync with the data?
消息框是否导致UI与数据不同步?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.