Source Link: MSDN
---
Hi,
basically I stumbled across this issue:
https://bengribaudo.com/blog/2010/09/14/199/keeping-selected-item-selected-when-changing-listviews-itemssource/comment-page-1?unapproved=88417&moderation-hash=4f244e91363349e3b625ff44be716cb2#comment-88417
I've got a ListView that I want to sort. I want to keep the selecteditem selected after the sorting operation. The problem I have is that the selecteditem is reset for a very small time to null, that I want to avoid, because other bindings rely on this property. This issue is described in the last part of the post, but not solved.
I tried to add an additional _isSorting boolean, without luck:
private void SortConversations()
{
_isSorting = true;
_conversations.Sort(model => model.LastMessageTimeStamp, descending: true);
_isSorting = false;
}
public ConversationViewModel SelectedConversation
{
get => _selectedConversation;
set
{
if (!_isSorting)
{
Set(ref _selectedConversation, value);
}
}
}
The problem is, that the Item does not appear as selected in the UI. Even if I try to manually set it again, it requires some additional Task.Delay() between the sort and the (re)selection. That is no proper solution, especially if the pc running the app is slower or faster.
---
I've already tried all the solutions you described. I am using an observable collection and Move() the items. I've also tried to backup the SelectedItem, but as I said it is null for a small time amount. And If I reselect the item, I need to add a delay between the Sorting and the reselection. And that is exactly the problem I am facing. How can I solve this?
I've prepared a minimal example, so you can test the problem real quick:
https://github.com/AtosNicoS/ListViewSortIssue
To reproduce the issue:
- Click "Reset" Button
- Select any item
- Click "Sort" Button
- Watch and Repeat
What you will see is, that sometimes (50% of the time) after the sorting a wrong(!!) item gets selected. With the additional delay, the correct item will be selected after one second. That is one issue I am focussing.
The other issue is, that SelectedItem is reset to null while sorting the list. I've added code to work around this issue with the _isSorting boolean. It is currently not activated, but should work commented out. You said, observable collections should work better here - they do not. The value is still null. And the issue even becomes more complicated, combined with the delay issue described above.
In my real application (with more data and larger datatemplates) this issue becomes even more complicated. If the selected item is at the top of the list, the sorting+selection works without delay, if it is in the bottom of the list, the item is not selected after sorting anymore.
Adding a delay does not sound like the right solution. I somehow need to wait until the ListView refreshed its items.
Any ideas?