WindowsCommunityToolkit icon indicating copy to clipboard operation
WindowsCommunityToolkit copied to clipboard

[Bug] Exception is thrown when removing the last item in a group in ObservableGroupedCollection bound to a DataGrid

Open phizch opened this issue 2 years ago • 2 comments

Description

When the item at the last index of an ObservableGroup that is in an ObservableGroupedCollection bound via a CollectionViewSource to a DataGrid is removed, an ArgumentOutOfRangeExcpetion is thrown.

Short example:

var list = new List<int> { 1, 1, 1, 2, 3 };
var ogc = new ObservableGroupedCollection<int,int>( list.GroupBy( x => x ) );
var cvs = new CollectionViewSource { Source = ogc, IsSourceGrouped = true };
ogc[0].RemoveAt( ogc[0].Count-1 ); // works fine
var myDataGrid = new DataGrid();
myDataGrid.ItemsSource = cvs.View;
ogc[0].RemoveAt( ogc[0].Count-1 ); // ArgumentOutOfRangeExcpetion

Expected behavior

No exception should be thrown. The DataGrid should remove the row, and if the row was selected, either select the next or previous row, or set selection to empty.

Partial fix

The DataGrid should check that the index is still valid before trying to fetch the value from the ObservableGroup.

Workaround

Wrapping the Remove or RemoveAt in a try/catch block and catching the ArgumentOutOfRangeExcpetion mitigates the issue.

try
{
    ogc[0].RemoveAt( ogc[0].Count-1 ); // ArgumentOutOfRangeExcpetion
}
catch ( ArgumentOutOfRangeExcpetion )
{
    //todo: log the exception
}

phizch avatar Feb 01 '22 12:02 phizch

Hello phizch, 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 🙌

ghost avatar Feb 01 '22 12:02 ghost

This is related or duplicate of #2913 and #3924 I believe. There's some open PRs/work in this space, but I believe we were waiting on a more performant solution and some more testing.

michael-hawker avatar Feb 11 '22 20:02 michael-hawker