CommunityToolkit / WindowsCommunityToolkit

The Windows Community Toolkit is a collection of helpers, extensions, and custom controls. It simplifies and demonstrates common developer tasks building .NET apps with UWP and the Windows App SDK / WinUI 3 for Windows 10 and Windows 11. The toolkit is part of the .NET Foundation.
https://docs.microsoft.com/windows/communitytoolkit/
Other
5.87k stars 1.38k forks source link

AdvancedCollectionView ArgumentOutOfRangeException when remove the last element #4263

Open Dimigergo opened 2 years ago

Dimigergo commented 2 years ago

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.

Steps to Reproduce

Steps to reproduce the behavior:

  1. AdvancedCollectionView.zip
  2. Start application
  3. Click on 'Delete last'
  4. 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:

App min and target version:

Device form factor:

Visual Studio version:

ghost commented 2 years ago

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 🙌

michael-hawker commented 2 years ago

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.

Mirofire commented 1 year ago

Is this issue still open? I getting the same issue with Nuget package CommunityToolkit.WinUI.UI.Controls Version: 7.1.2