开发者

How do I keep the selection in ListBox when reloading ItemsSource

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 Cu开发者_JAVA技巧stomer

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 ?


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.


In case the reloaded list still contains the last selected item and you want that item to be selected, then you can raise the PropertyChange event for the property SelectedCustomer after your collection gets reloaded. Please make your sure your viewmodel class implements INotifyPropertyChanged interface.


you can use the ICollectionView to select the entity you want.

ICollectionview view = (ICollectionView)CollectionViewSource.GetDefaultView(this.Customers);

view.MoveCurrentTo(SelectedCustomer);

in your Xaml the itemsControl must have IsSynchronizedWithCurrentItem=true

or if the ItemsControl has a SelectedItem property you can simply bind it to your SelectedCustomer Property.


When you "reload" your collection you basically replace all values in it with new values. Even those that look and feel identical are in fact new items. So how do you want to reference the same item in the list when it is gone? You could certainly use a hack where you determine the item that was selected by its properties and reselect it (i.e. do a LINQ search through the list and return the ID of the matching item, then reselect it). But that would certainly not be using best practices.

You should really only update your collection, that is remove invalid entried and add new entries. If you have a view connected to your collection all the sorting and selecting and whatnot will be done automagically behind the scenes again.

Edit:

        var tmp = this.listBox1.SelectedValue;
        this._customers.Clear();

        this._customers.Add(item1); this._customers.Add(item2);
        this._customers.Add(item3); this._customers.Add(item4);
        this.listBox1.SelectedValue = tmp;

in the method that does the reset/clear works for me. I.e. that is the code I put into the event handling method called when pressing the refresh button in my sample app. That way you dont even need to keep references to the customer objects as long as you make sure that whatever your ID is is consistent. Other things I have tried, like overwriting the collections ´ClearItems()´ method and overwriting ´Equals()´ and ´GetHashCode()´ didn't work - as I expected.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜