From 4f83a1c36b1ae317fed1a618b3e9eb845bce17eb Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Wed, 27 Dec 2023 01:23:14 -0600 Subject: [PATCH] fix new SortedColumnIndex generation --- Maui.DataGrid/DataGrid.xaml.cs | 23 ++++++----------------- 1 file changed, 6 insertions(+), 17 deletions(-) diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs index 50f2de5..52022ce 100644 --- a/Maui.DataGrid/DataGrid.xaml.cs +++ b/Maui.DataGrid/DataGrid.xaml.cs @@ -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) { @@ -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().ToList(); - var newColumns = e.NewItems?.Cast().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;