Replies: 4 comments
-
Before sorting, you could store a reference to the selected item and set it back as the selected item after the sorting is done |
Beta Was this translation helpful? Give feedback.
-
No, it would temporary set the value to null, which will cause my logic to break. Isnt there a way to properly support move operations on the binded collection? |
Beta Was this translation helpful? Give feedback.
-
https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.notifycollectionchangedaction?view=net-5.0 ObservableCollection only has certain collection changed actions that get raised. Namely Add, Move, Remove, Replace, and reset. If you change multiple items in your observable collection at the same time the only action that makes sense is a reset. I think list view assumes that on collection reset it should clear its selected index's. However I assume if the IsSelected property on one of its elements is true that should be honored... @ranjeshj or maybe @jevansaks or @anawishnoff do you know the intended behavior? |
Beta Was this translation helpful? Give feedback.
-
I even tried a manual sorting implementation which only does move operations, and also shifts around the selected item, without any luck. Here is the extension method: public static void Sort<TSource, TKey>(this ObservableCollection<TSource> source, Func<TSource, TKey> keySelector, TSource keepItem, bool descending = false, bool useMove = true)
{
var sortedSource = descending
? source.OrderByDescending(keySelector).ToList()
: source.OrderBy(keySelector).ToList();
// This is the index where the item we do not want to involve any move operation should be placed later
// NOTE: Also works if keepItem is null.
var keepItemNewIndex = sortedSource.IndexOf(keepItem);
// First we place all elements before the item to keep
for (var i = 0; i < keepItemNewIndex; i++)
{
var itemToSort = sortedSource[i];
// If the item is already at the right position, leave it and continue.
var index = source.IndexOf(itemToSort);
if (index == i)
{
continue;
}
if (useMove)
{
source.Move(index, i);
}
else
{
var item = source[index];
source.Remove(item);
source.Insert(i, item);
}
}
// Second we place all elements to the end of the list.
for (var i = sortedSource.Count - 1; i > keepItemNewIndex; i--)
{
var itemToSort = sortedSource[i];
// If the item is already at the right position, leave it and continue.
var index = source.IndexOf(itemToSort);
if (index == i)
{
continue;
}
if (useMove)
{
source.Move(index, i);
}
else
{
var item = source[index];
source.Remove(item);
source.Insert(i, item);
}
}
} |
Beta Was this translation helpful? Give feedback.
-
I dont want to repeat myself, so please refer to: https://docs.microsoft.com/en-us/answers/questions/3190/sorting-listview-resets-the-selecteditem-property.html?childToView=345957
The problem basically is that the
SelectedItem
will be reset on listview sorting when theIsSelected
Property of theListViewItem
is also binded to the ViewModel. I need this binding as aTwoWay
binding, so the suggested solution from the forum will not help.Here is a minimal example:
https://github.com/AtosNicoS/ListViewSortIssue
Beta Was this translation helpful? Give feedback.
All reactions