When Clearing an ObservableCollection, There are No Items in e.OldItems

前端 未结 20 1809
不知归路
不知归路 2020-11-30 00:27

I have something here that is really catching me off guard.

I have an ObservableCollection of T that is filled with items. I also have an event handler attached to t

20条回答
  •  旧时难觅i
    2020-11-30 00:59

    We had the same issue here. The Reset action in CollectionChanged does not include the OldItems. We had a workaround: we used instead the following extension method:

    public static void RemoveAll(this IList list)
    {
       while (list.Count > 0)
       {
          list.RemoveAt(list.Count - 1);
       }
    }
    

    We ended up not supporting the Clear() function, and throwing a NotSupportedException in CollectionChanged event for Reset actions. The RemoveAll will trigger a Remove action in CollectionChanged event, with the proper OldItems.

提交回复
热议问题