Changing collection from CollectionChanged event
I want to update collection after it was changed but I can't seem to get "away" from this exception:
Cannot change ObservableCollection during a CollectionChanged or PropertyChanged even开发者_开发知识库t.
Inside event handler I unsubscribe from Collection changed event before changing anything to prevent infinite loops and after changes are made i subscribe again to same event.
private void CollectionChanged(object sender, NotifyCollectionChangedEventArgs e)
{
data.CollectionChanged -= CollectionChanged;
data.Add("Item");
data.CollectionChanged += CollectionChanged;
}
I tried using Dispatcher to call data.Add("Item"), but no luck :(
The problem is that you are unsubscribing from the event within the event which has yet to complete. Drop back and re-evaluate why you are adding to the collection and determine if there is another way to accomplish what you need.
精彩评论