From 89db8bca05feb3e368220e80dda2fb10c6b8afdb Mon Sep 17 00:00:00 2001 From: Edward Miller Date: Wed, 27 Dec 2023 13:21:44 -0600 Subject: [PATCH] prevent simultaneous sorting --- Maui.DataGrid/DataGrid.xaml.cs | 61 ++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 21 deletions(-) diff --git a/Maui.DataGrid/DataGrid.xaml.cs b/Maui.DataGrid/DataGrid.xaml.cs index e20b0fc..2ec65a1 100644 --- a/Maui.DataGrid/DataGrid.xaml.cs +++ b/Maui.DataGrid/DataGrid.xaml.cs @@ -33,7 +33,9 @@ public partial class DataGrid private readonly Style _defaultSortIconStyle; private bool _isReloading; + private bool _isSortingAndPaginating; private readonly object _reloadLock = new(); + private readonly object _sortAndPaginateLock = new(); private IList? _internalItems; private DataGridColumn? _sortedColumn; @@ -175,33 +177,50 @@ private IEnumerable GetPaginatedItems(IEnumerable unpaginatedIte private void SortAndPaginate(SortData? sortData = null) { - if (ItemsSource is null) + lock (_sortAndPaginateLock) { - return; - } + if (_isSortingAndPaginating) + { + return; + } - sortData ??= SortedColumnIndex; + _isSortingAndPaginating = true; - var originalItems = ItemsSource.Cast().ToList(); + try + { + if (ItemsSource is null) + { + return; + } - IList sortedItems; + sortData ??= SortedColumnIndex; - if (sortData != null && CanSort(sortData)) - { - sortedItems = GetSortedItems(originalItems, sortData); - } - else - { - sortedItems = originalItems; - } + var originalItems = ItemsSource.Cast().ToList(); - if (PaginationEnabled) - { - InternalItems = GetPaginatedItems(sortedItems).ToList(); - } - else - { - InternalItems = sortedItems; + IList sortedItems; + + if (sortData != null && CanSort(sortData)) + { + sortedItems = GetSortedItems(originalItems, sortData); + } + else + { + sortedItems = originalItems; + } + + if (PaginationEnabled) + { + InternalItems = GetPaginatedItems(sortedItems).ToList(); + } + else + { + InternalItems = sortedItems; + } + } + finally + { + _isSortingAndPaginating = false; + } } }