diff --git a/src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs b/src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs index 988e5334560e..55d91f386781 100644 --- a/src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs +++ b/src/Controls/src/Core/Handlers/Items/SelectableItemsViewHandler.Windows.cs @@ -49,8 +49,8 @@ protected override void DisconnectHandler(ListViewBase platformView) if (oldListViewBase != null) { - oldListViewBase.ClearValue(ListViewBase.SelectionModeProperty); oldListViewBase.SelectionChanged -= PlatformSelectionChanged; + oldListViewBase.ClearValue(ListViewBase.SelectionModeProperty); } if (ItemsView != null) diff --git a/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs b/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs index 7475ce81c340..bbb673174f92 100644 --- a/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs +++ b/src/Controls/tests/DeviceTests/Elements/CollectionView/CollectionViewTests.Windows.cs @@ -1,4 +1,5 @@ -using System.Collections.ObjectModel; +using System.Collections.Generic; +using System.Collections.ObjectModel; using System.Linq; using System.Threading.Tasks; using Microsoft.Maui.Controls; @@ -58,6 +59,51 @@ await CreateHandlerAndAddToWindow(layout, (handler) => }); } + [Fact(DisplayName = "CollectionView Disconnects Correctly with MultiSelection")] + public async Task CollectionViewHandlerDisconnectsWithMultiSelect() + { + SetupBuilder(); + + ObservableCollection data = new ObservableCollection() + { + "Item 1", + "Item 2", + "Item 3" + }; + + var collectionView = new CollectionView() + { + ItemTemplate = new Controls.DataTemplate(() => + { + return new VerticalStackLayout() + { + new Label() + }; + }), + SelectionMode = SelectionMode.Multiple, + ItemsSource = data + }; + + var layout = new VerticalStackLayout() + { + collectionView + }; + + await CreateHandlerAndAddToWindow(layout, (handler) => + { + collectionView.SelectedItems.Add(data[0]); + collectionView.SelectedItems.Add(data[2]); + + // Validate that no exceptions are thrown + var collectionViewHandler = (IElementHandler)collectionView.Handler; + collectionViewHandler.DisconnectHandler(); + + ((IElementHandler)handler).DisconnectHandler(); + + return Task.CompletedTask; + }); + } + [Fact] public async Task ValidateItemContainerDefaultHeight() {