将改变的模型列表同步到ViewModel的ObservableList的最佳实践? [英] Best practice for synchronizing a changing List of the model to a ObservableList of the ViewModel?

查看:265
本文介绍了将改变的模型列表同步到ViewModel的ObservableList的最佳实践?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个(外部)模型暴露一个不断变化的列表(比方说每两秒钟)。
ViewModel知道注册PropertyChange事件的列表。 ViewModel还为UI提供了一个ObservableCollection用于数据绑定。

I have an (external) model exposing a List that constantly changes (let's say every two seconds or so). A ViewModel knows that list registering for PropertyChange events. That ViewModel also provides an ObservableCollection to the UI for data binding.

+-----------------------------------------------+
|                                           View|
| +-----------+                                 |
| |Listbox    |                                 |
| +-----------+                                 |
+-----/\----------------------------------------+
      ||
      ||DataBinding
      ||
      ||
+-----||----------------------------------------+
|     ||                               ViewModel|
| +--------------------+         +-------------+|
| |ObservableCollection|<--------|ChangeHandler||
| +--------------------+    /    +-------------+|
|                          /           ^        |
+-------------------------/------------|--------+
                         /             |
                        /              |
           Synchronizing Lists         | PropertyChanged
                                       |
                                       |
+--------------------------------------|--------+
|                                  +-----+ Model|
|                                  |IList|      |
|                                  +-----+      |
|                                               |
+-----------------------------------------------+

原则上,除了不断更新外,随着每次更新,用户放弃他的选择,即所有项目将被取消选择每次更新。
这不奇怪,因为WPF的ListBox看到一个新的列表被分配。

In principle that works well, besides the updates conducted constantly. With every update the user looses his selection, i.e. all items will be deselected with every update. This is no wonder, as WPF's ListBox "sees" that a new list was assigned.

所以,事情必须是我们做分配新的ObservableCollection,但将当前ObservableCollection的内容与更新的Model.List合并

So, the thing must be that we do not assign a new ObservableCollection, but merge the content of the current ObservableCollection with the updated Model.List

现在我的问题


  • 同步列表 - 有关如何进行合并的最佳做法(或框架)
    (将新项目复制到ObservableCollection,删除缺失的项目,更新更改的项目)

  • 所选项目 - 如何确保ListBox保留当前所选项目(除了该项目已删除的情况)

推荐答案

您可以从更新的模型列表生成一个新的ObservableCollection,或者使用模型的同步当前的ObservableCollection。

You can either generate a new ObservableCollection from the updated model list or sync the current ObservableCollection with the model's one.

如果你去第二个,你可能想避免的是为每个同步的项目启动CollectionChanged事件。请查看此ObservableCollection实施,该实现可以延迟通知。

If you go for the second, one thing you may want to avoid is to fire CollectionChanged events for every synced item. Take a look at this ObservableCollection implementation which has the hability to defer the notifications.

对于保持当前的SelectedItem,如果没有改变ObservableCollection的实例(什么是true,因为我们正在同步集合)和SelectedItem实例isn' t删除,列表框应该保存选择。然而,我不确定如果这是真的如果NotifyCollectionChangedEventArgs.Action是重置。如果是这样,您可以使用我使用的方法,这是在ViewModel中同时具有colllection属性和SelectedItem属性。在TwoWay模式下将ViewModel的SelectedItem绑定到ListBox.SelectedItem。当同步集合时,将SelectedItem保存在temp变量中,然后在同步后重新应用它(如果未删除)。

As for keeping the current SelectedItem, if the instance of the ObservableCollection isn't changed (what is true, because we are syncing the collections) and the SelectedItem instance isn't removed, the listbox should hold the selection. However, I'm not certain if that is true if the NotifyCollectionChangedEventArgs.Action is "Reset". If that's the case, you can use the approach I use, which is to have both a colllection property and a SelectedItem property in the ViewModel. You bind the ViewModel's SelectedItem to the ListBox.SelectedItem in TwoWay mode. When you synchronize the Collections, you save the SelectedItem in a temp variable and then reapply it, if it wasn't removed, after the synchronization.

这篇关于将改变的模型列表同步到ViewModel的ObservableList的最佳实践?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆