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

[Bug]: SortAndBind() method will never create a NotifyCollectionChangedAction.Move in the resulting (observable) list when sorting order changes (#934) #936

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions src/DynamicData/Binding/SortAndBind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,7 @@ private void ApplyChanges(IChangeSet<TObject, TKey> changes)
updatedIndex = currentIndex < updatedIndex ? updatedIndex - 1 : updatedIndex;
if (updatedIndex != currentIndex)
{
target.RemoveAt(currentIndex);
target.Insert(updatedIndex, item);
target.Move(currentIndex, updatedIndex, item);
}
}
break;
Expand Down
20 changes: 20 additions & 0 deletions src/DynamicData/Cache/Internal/SortExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
// Roland Pheasant licenses this file to you under the MIT license.
// See the LICENSE file in the project root for full license information.

using System.Collections.ObjectModel;

namespace DynamicData.Cache.Internal;

internal static class SortExtensions
Expand Down Expand Up @@ -51,4 +53,22 @@ public static int GetInsertPositionLinear<TItem>(this IList<TItem> list, TItem t

return list.Count;
}

public static void Move<TItem>(this IList<TItem> list, int original, int destination, TItem item)
{
// If the list supports the Move method, use it instead of removing and inserting.
if (list is IExtendedList<TItem> extendedList)
{
extendedList.Move(original, destination);
}
else if (list is ObservableCollection<TItem> observableList)
{
observableList.Move(original, destination);
}
else
{
list.RemoveAt(original);
list.Insert(destination, item);
}
}
}
Loading