Skip to content

Commit

Permalink
fix new SortedColumnIndex generation
Browse files Browse the repository at this point in the history
  • Loading branch information
Edward Miller committed Dec 27, 2023
1 parent cd7bb07 commit 4f83a1c
Showing 1 changed file with 6 additions and 17 deletions.
23 changes: 6 additions & 17 deletions Maui.DataGrid/DataGrid.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -992,7 +992,7 @@ protected override void OnBindingContextChanged()

private void OnColumnsChanged(object? sender, NotifyCollectionChangedEventArgs e)
{
var newSortedColumnIndex = RegenerateSortedColumnIndex(e);
var newSortedColumnIndex = RegenerateSortedColumnIndex();

if (newSortedColumnIndex != SortedColumnIndex)
{
Expand All @@ -1014,29 +1014,18 @@ private void OnSelectionChanged(object? sender, SelectionChangedEventArgs e)
_itemSelectedEventManager.HandleEvent(this, e, nameof(ItemSelected));
}

private SortData? RegenerateSortedColumnIndex(NotifyCollectionChangedEventArgs e)
private SortData? RegenerateSortedColumnIndex()
{
var oldColumns = e.OldItems?.Cast<DataGridColumn>().ToList();
var newColumns = e.NewItems?.Cast<DataGridColumn>().ToList();

oldColumns?.ForEach(oldColumn => oldColumn.SizeChanged -= OnColumnSizeChanged);
newColumns?.ForEach(newColumn => newColumn.SizeChanged += OnColumnSizeChanged);

int? newSortedColumnIndex = null;

if (newColumns != null && _sortedColumn != null)
{
newSortedColumnIndex = newColumns.IndexOf(_sortedColumn);
}

if (_sortedColumn != null && SortedColumnIndex != null)
{
if (newSortedColumnIndex is null or -1)
var newSortedColumnIndex = Columns.IndexOf(_sortedColumn);

if (newSortedColumnIndex == -1)
{
return null;
}

return new(newSortedColumnIndex.Value, SortedColumnIndex.Order);
return new(newSortedColumnIndex, SortedColumnIndex.Order);
}

return SortedColumnIndex;
Expand Down

0 comments on commit 4f83a1c

Please sign in to comment.