开发者

Changing collection from CollectionChanged event

开发者 https://www.devze.com 2023-01-07 16:00 出处:网络
I want to update collection after it was changed but I can\'t seem to get \"away\" from this exception:

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消