操作 ObservableCollection 与替换列表

发布于 2024-08-23 09:01:19 字数 577 浏览 12 评论 0原文

我有一个用于同步的后端字典(即同步到文件存储和网络服务)。

除此之外,我需要生成列表/枚举供 WPF 前端使用。将可枚举项挂接到字典,与在更新为使用 ObservableCollection 时调用 PropertyChanged 并让它自动调用其 CollectionChanged 之间有什么区别。

同步在后台自动进行,某些元素可能会被删除,其他元素可能会被更新。我想顺利地将这些信息传播到 WPF 前端和用户。 (即,如果删除一项,则不必重新初始化整个显示)。我还想在添加和删除项目时添加动画(即淡入和淡出) - 如果我替换整个列表,这可能吗?还是会导致每个项目再次淡入?

那么我应该:

1)使用可观察集合并在字典和集合之间编写一些奇特的同步逻辑吗?

2)使用linq扩展方法将字典转换为可枚举,并在可枚举发生变化时简单地调用propertychanged?

3)在字典和列表之间同步,通过在更新时替换列表来实现?

另外,这些如何与仅为 UI 执行的排序和过滤操作配合使用? (即,如果我需要根据用户选择从字典中过滤某些元素,我是否应该使用与您推荐的类似的方法?)

I have a backend Dictionary that is used for synchronization (ie. to both a filestore and a webservice).

Off the top of this I need to generate lists/enumerables for the WPF frontend to consume. What is the difference between either hooking an enumerable up to the dictionary, and calling PropertyChanged when it is updated to using an ObservableCollection and having it automatically called its CollectionChanged.

Synchronizing occurs in the background automatically, and some elements may be removed, others may be updated. I want to propagate this information to the WPF frontend and user smoothly. (ie. if one item is removed, the whole display shouldn't have to be reinitialized). I also want to add animation when items are added and removed (ie. fade in and out) - is this possible if I replace the whole list or will it cause every single item to fade in again?

So should I:

1) use an observable collection and write some fancy synchronization logic between the dictionary and the collection?

2) use linq extension methods to convert the dictionary to an enumerable and simply call propertychanged on the enumerable whenever it changes?

3) synchronize between a dictionary and a list, by replacing the list whenever it is updated?

Also, how would any of these work with sorting and filtering operations that are performed just for the UI? (ie. if I need to filter some elements out of the dictionary based upon user selection, should I use a similar method as the one you have recommended?)

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

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

发布评论

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

评论(1

寄居人 2024-08-30 09:01:19

如果您在发生更改时“替换”任何 IEnumerable,则整个列表将在 UI 中刷新。

为了避免这种情况,您需要实现 INotifyCollectionChanged,并提供一个实现此功能的集合。您无需替换集合,而是处理元素,从而触发相应的事件。

ObservableCollection 会为您处理这个问题。就个人而言,如果您需要将其保存在字典中,但希望将其同步到列表,您可能需要考虑创建一个自定义集合,可能基于 SortedDictionary。标准字典没有顺序意识,这意味着无法适当地实现 INotifyCollectionChanged。

If you "replace" any IEnumerable<T> when you get a change, the entire list will be refreshed in the UI.

In order to avoid that, you'll need to implement INotifyCollectionChanged, and provide a collection which implements this. Instead of replacing the collection, you handle the elements, which in turn fires the appropriate events.

ObservableCollection<T> handles this for you. Personally, if you need to keep this in a dictionary, but want to synchronize it to a list, you may want to consider making a custom collection, possibly based around SortedDictionary. A standard Dictionary has no sense of ordering, which means that there'd be no way to implement INotifyCollectionChanged appropriately.

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