WindowsCommunityToolkit
WindowsCommunityToolkit copied to clipboard
AdvancedCollectionView ArgumentOutOfRangeException when remove the last element
Describe the bug
When I use an AdvancedCollectionView and remove the last element from the bound collection the following exception thrown: 'System.ArgumentOutOfRangeException' in System.Private.CoreLib.dll Index was out of range. Must be non - negative and less than the size of the collection.
- [ ] Is this bug a regression in the toolkit?
Steps to Reproduce
- [x] Can this be reproduced in the Sample App?
Steps to reproduce the behavior:
- AdvancedCollectionView.zip
- Start application
- Click on 'Delete last'
- See error in message box.
Expected behavior
The last element should be deleted without error.
Environment
NuGet Package(s): Package Version(s):
<PackageReference Include="Microsoft.NETCore.UniversalWindowsPlatform">
<Version>6.2.12</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp">
<Version>7.1.0-rc2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp.UI">
<Version>7.1.0-rc2</Version>
</PackageReference>
<PackageReference Include="Microsoft.Toolkit.Uwp.UI.Controls">
<Version>7.1.0-rc2</Version>
</PackageReference>
Windows 10 Build Number:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [x] May 2020 Update (19041)
- [ ] Insider Build ({build_number})
App min and target version:
- [ ] Fall Creators Update (16299)
- [ ] April 2018 Update (17134)
- [ ] October 2018 Update (17763)
- [ ] May 2019 Update (18362)
- [x] May 2020 Update (19041)
- [ ] Insider Build ({build_number})
Device form factor:
- [x] Desktop
- [ ] Xbox
- [ ] Surface Hub
- [ ] IoT
Visual Studio version:
- [ ] 2017 (15.{minor_version})
- [x] 2019 (16.11.3)
- [ ] 2022 (17.{minor_version})
Hello Dimigergo, thank you for opening an issue with us!
I have automatically added a "needs triage" label to help get things started. Our team will analyze and investigate the issue, and escalate it to the relevant team if possible. Other community members may also look into the issue and provide feedback 🙌
Think this is a duplicate of #2913, thanks for providing a minimal repro project @Dimigergo.
Stack:
System.Private.CoreLib.dll!System.ThrowHelper.ThrowArgumentOutOfRange_IndexException() Unknown
Microsoft.Toolkit.Uwp.UI.dll!Microsoft.Toolkit.Uwp.UI.AdvancedCollectionView.this[int].get(int index) Line 214 C#
Microsoft.Toolkit.Uwp.UI.Controls.DataGrid.dll!Microsoft.Toolkit.Uwp.UI.Controls.DataGridInternals.DataGridDataConnection.NotifyingDataSource_VectorChanged(Windows.Foundation.Collections.IObservableVector<object> sender, Windows.Foundation.Collections.IVectorChangedEventArgs e) Line 957 C#
Microsoft.Toolkit.Uwp.UI.dll!Microsoft.Toolkit.Uwp.UI.AdvancedCollectionView.OnVectorChanged(Windows.Foundation.Collections.IVectorChangedEventArgs e) Line 65 C#
Microsoft.Toolkit.Uwp.UI.dll!Microsoft.Toolkit.Uwp.UI.AdvancedCollectionView.SourceNcc_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e) Line 629 C#
System.ObjectModel.dll!System.Collections.ObjectModel.ObservableCollection<AdvancedCollectionView.MainPage.CValami>.OnCollectionChanged(System.Collections.Specialized.NotifyCollectionChangedEventArgs e) Unknown
AdvancedCollectionView.exe!AdvancedCollectionView.MainPage.ButtonClick(object sender, Windows.UI.Xaml.RoutedEventArgs e) Line 91 C#
get: https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/760ab9ff25a7cf83a5ab6746411da6b2d832fe08/Microsoft.Toolkit.Uwp.UI/AdvancedCollectionView/AdvancedCollectionView.cs#L212-L216
removeAt: https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/760ab9ff25a7cf83a5ab6746411da6b2d832fe08/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/DataGrid/DataGridDataConnection.cs#L952-L958
OnVectorChanged: https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/760ab9ff25a7cf83a5ab6746411da6b2d832fe08/Microsoft.Toolkit.Uwp.UI/AdvancedCollectionView/AdvancedCollectionView.Events.cs#L55-L66
https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/760ab9ff25a7cf83a5ab6746411da6b2d832fe08/Microsoft.Toolkit.Uwp.UI/AdvancedCollectionView/AdvancedCollectionView.cs#L627-L639
https://github.com/CommunityToolkit/WindowsCommunityToolkit/blob/760ab9ff25a7cf83a5ab6746411da6b2d832fe08/Microsoft.Toolkit.Uwp.UI/AdvancedCollectionView/AdvancedCollectionView.cs#L741-L751
So, I think the issue here is DataGrid
is trying to retrieve the item to know which one is removed, but it's already been removed from the collection when the event is called. Normally with an INotifyCollectionChanged
event the item is provided in the event args, but that doesn't seem to be the case with the IVectorChangedEventArgs
If we swap the DataGrid
for a ListView
, things work fine. This seems to work fine in Wpf as they use the collectionchangedevent compared to an observablevector. FYI @RBrid.
Is this issue still open? I getting the same issue with Nuget package CommunityToolkit.WinUI.UI.Controls Version: 7.1.2