Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I keep the selection in ListBox when reloading ItemsSource

Tags:

c#

mvvm

wpf

I am experimenting with WPF and MVVM in our system. However iam having a problem with keeping things selected in lists using only MVVM ( without doing extra CollectionViews ). What i currently have is the list

ObservableCollection<ReservationCustomerList> Customers;

And then a property storing the selected Customer

ReservationCustomerList SelectedCustomer;

In my opinion now, when the list reloads (actually from another thread async), the selection should be able to be kept, however this does not happen.

Does someone have a nice clean way of achieving this ?

like image 808
Morten Schmidt Avatar asked Oct 26 '25 09:10

Morten Schmidt


1 Answers

The way we did it was that we did not replace the collection. We added/removed the entries and updated existing entries if required. This maintains the selection.

You can use LINQ methods like Except to identify items that are new or removed.

like image 95
Muhammad Hasan Khan Avatar answered Oct 28 '25 22:10

Muhammad Hasan Khan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!