diff --git a/src/Uno.UI/UI/Xaml/Controls/ItemCollection.cs b/src/Uno.UI/UI/Xaml/Controls/ItemCollection.cs index ed3787f133e0..7e7d22d44ad4 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ItemCollection.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ItemCollection.cs @@ -155,6 +155,7 @@ internal void SetItemsSource(IEnumerable itemsSource) if (itemsSource == null) { _itemsSource = null; + ObserveCollectionChanged(null); } else { @@ -182,7 +183,12 @@ internal void SetItemsSource(IEnumerable itemsSource) private void ObserveCollectionChanged(object itemsSource) { - if (itemsSource is INotifyCollectionChanged existingObservable) + if (itemsSource is null) + { + // fast path for null + _itemsSourceCollectionChangeDisposable.Disposable = null; + } + else if (itemsSource is INotifyCollectionChanged existingObservable) { // This is a workaround for a bug with EventRegistrationTokenTable on Xamarin, where subscribing/unsubscribing to a class method directly won't // remove the handler. diff --git a/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs b/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs index 323b99e3e57d..5856cf9fa768 100644 --- a/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs +++ b/src/Uno.UI/UI/Xaml/Controls/ItemsControl/ItemsControl.cs @@ -660,8 +660,12 @@ internal void ObserveCollectionChanged() { var unwrappedSource = UnwrapItemsSource(); + if(unwrappedSource is null) + { + _notifyCollectionChanged.Disposable = null; + } //Subscribe to changes on grouped source that is an observable collection - if (unwrappedSource is CollectionView collectionView && collectionView.CollectionGroups != null && collectionView.InnerCollection is INotifyCollectionChanged observableGroupedSource) + else if (unwrappedSource is CollectionView collectionView && collectionView.CollectionGroups != null && collectionView.InnerCollection is INotifyCollectionChanged observableGroupedSource) { // This is a workaround for a bug with EventRegistrationTokenTable on Xamarin, where subscribing/unsubscribing to a class method directly won't // remove the handler.