重新加载 ItemsSource 时如何保留 ListBox 中的选择

发布于 2024-12-06 14:44:11 字数 358 浏览 1 评论 0原文

我正在我们的系统中试验 WPF 和 MVVM。然而,我在仅使用 MVVM (不执行额外的 CollectionViews )在列表中保留所选内容时遇到了问题。 我当前拥有的是列表

ObservableCollection<ReservationCustomerList> Customers;

,然后是存储所选客户的属性

ReservationCustomerList SelectedCustomer;

现在我认为,当列表重新加载时(实际上是从另一个线程异步),选择应该能够保留,但是这不会发生。

有人有一个干净的方法来实现这一目标吗?

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 ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

野の 2024-12-13 14:44:11

我们的做法是不更换集合。我们添加/删除了条目,并根据需要更新了现有条目。这将维持选择。

您可以使用 except 等 LINQ 方法来标识新项目或已删除项目。

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.

九局 2024-12-13 14:44:11

如果重新加载的列表仍包含最后选定的项目,并且您希望选择该项目,则可以在集合重新加载后引发属性 SelectedCustomerPropertyChange 事件。
请确保您的 viewmodel 类实现了 INotifyPropertyChanged 接口。

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.

儭儭莪哋寶赑 2024-12-13 14:44:11

您可以使用 ICollectionView 来选择所需的实体。

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

view.MoveCurrentTo(SelectedCustomer);

在 Xaml 中,itemsControl 必须具有 IsSynchronizedWithCurrentItem=true

,或者如果 ItemsControl 具有 SelectedItem 属性,您只需将其绑定到 SelectedCustomer 属性即可。

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.

〆一缕阳光ご 2024-12-13 14:44:11

当您“重新加载”集合时,您基本上将其中的所有值替换为新值。即使那些外观和感觉相同的产品实际上也是新产品。那么,当列表中的同一项目消失时,您如何引用它呢?您当然可以使用 hack,确定通过其属性选择的项目并重新选择它(即通过列表进行 LINQ 搜索并返回匹配项目的 ID,然后重新选择它)。但这肯定不是使用最佳实践。

您实际上应该只更新您的集合,即删除无效条目并添加新条目。如果您有一个连接到您的集合的视图,那么所有排序和选择之类的操作都将在幕后自动完成。

编辑:

        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;

在重置/清除对我有用的方法中。即,这是我放入示例应用程序中按下刷新按钮时调用的事件处理方法中的代码。这样,您甚至不需要保留对客户对象的引用,只要您确保您的 ID 是一致的即可。我尝试过的其他方法,例如覆盖集合“ClearItems()”方法以及覆盖“Equals()”和“GetHashCode()”都不起作用 - 正如我所预期的那样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文