Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AdvancedCollectionView ArgumentOutOfRangeException when remove the last element #4263

Open
5 of 21 tasks
Dimigergo opened this issue Sep 21, 2021 · 3 comments · May be fixed by #4360
Open
5 of 21 tasks

AdvancedCollectionView ArgumentOutOfRangeException when remove the last element #4263

Dimigergo opened this issue Sep 21, 2021 · 3 comments · May be fixed by #4360
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control In-PR 🚀

Comments

@Dimigergo
Copy link

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

  • Can this be reproduced in the Sample App?

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:

  • Fall Creators Update (16299)
  • April 2018 Update (17134)
  • October 2018 Update (17763)
  • May 2019 Update (18362)
  • 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)
  • May 2020 Update (19041)
  • Insider Build ({build_number})

Device form factor:

  • Desktop
  • Xbox
  • Surface Hub
  • IoT

Visual Studio version:

  • 2017 (15.{minor_version})
  • 2019 (16.11.3)
  • 2022 (17.{minor_version})
@Dimigergo Dimigergo added the bug 🐛 An unexpected issue that highlights incorrect behavior label Sep 21, 2021
@ghost ghost added the needs triage 🔍 label Sep 21, 2021
@ghost
Copy link

ghost commented Sep 21, 2021

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
Copy link
Member

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:

public object this[int index]
{
get { return _view[index]; }
set { _view[index] = value; }
}

removeAt:

case CollectionChange.ItemRemoved:
if (!this.IsGrouping)
{
// If we're grouping then we handle this through the CollectionViewGroup notifications.
// Remove is a single item operation.
_owner.RemoveRowAt(index, sender[index]);
}

OnVectorChanged:

private void OnVectorChanged(IVectorChangedEventArgs e)
{
if (_deferCounter > 0)
{
return;
}
VectorChanged?.Invoke(this, e);
// ReSharper disable once ExplicitCallerInfoArgument
OnPropertyChanged(nameof(Count));
}

case NotifyCollectionChangedAction.Remove:
DetachPropertyChangedHandler(e.OldItems);
if (_deferCounter <= 0)
{
if (e.OldItems?.Count == 1)
{
HandleItemRemoved(e.OldStartingIndex, e.OldItems[0]);
}
else
{
HandleSourceChanged();
}
}

private void RemoveFromView(int itemIndex, object item)
{
_view.RemoveAt(itemIndex);
if (itemIndex <= CurrentPosition)
{
CurrentPosition--;
}
var e = new VectorChangedEventArgs(CollectionChange.ItemRemoved, itemIndex, item);
OnVectorChanged(e);
}

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
Copy link

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug 🐛 An unexpected issue that highlights incorrect behavior DataGrid 🔠 Issues on DataGrid control In-PR 🚀
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants