如何使用集合构建 MVVM? [英] How do I structure MVVM with Collections?

查看:50
本文介绍了如何使用集合构建 MVVM?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当涉及到列表/集合时,我无法理解如何应用 MVVM 模式.

I'm having trouble understanding how to apply the MVVM pattern when Lists/Collections are involved.

假设 MainModel 有一些属性和方法,以及一个包含其他 DetailModel 对象的列表.可以添加、删除或重新排序 DetailModel 对象.

Say the MainModel has a few properties and methods, as well as a list that contains other DetailModel objects. The DetailModel objects can be added, removed, or re-ordered.

MainView 将显示一些与根模型相关的控件,并从列表中填充一个 ListBox.每个项目都将通过 DetailModelView UserControl 拥有自己的子视图.

The MainView will show a few controls related the the root model, and have a ListBox populated from the list. Each item will have it's own sub-view via a DetailModelView UserControl.

最后,有一个 MainViewModel.这具有由 MainModel 的属性和方法支持的属性,绑定到主视图,更改通知使所有内容保持同步.(到目前为止,我对这种模式感到满意——如果我遗漏了一些基本的东西,请多加说明......)

Finally, there is a MainViewModel. This has properties backed by the MainModel's properties and methods, bound to the Main View, with change notification keeping everything in sync. (Up to this point, I am comfortable with the pattern - more stating this in case there is something fundamental I am missing...)

谈到处理列表时,我感到很困惑.我遇到过几个例子,其中 MainViewModel 只是将 DetailModels 的列表暴露给视图,而 DetailModelViews 直接绑定到模型.这功能,但有问题.它没有始终遵循模式(不存在 DetailViewModel),并且它促使我在我的细节模型中包含一些与 UI 相关的代码.我似乎很清楚 MainViewModel 应该公开一个 DetailViewModels 列表以供 UI 绑定,但我一直在思考如何实现这样的事情!

When it comes to handling the list, I get confused. I have come across several examples where the MainViewModel simply exposes the list of DetailModels to the view, and the DetailModelViews are bound directly to the models. This functions, but is problematic. It does not consistently following the pattern (no DetailViewModel exists), and it drives me to include some UI-related code in my detail models. It seems clear to me that the MainViewModel should expose a list of DetailViewModels for the UI to bind, but I am stuck on how to implement such a thing!

应该如何管理这两个列表(DetailModelsDetailViewModels)?我真的很困惑我最初填充 DetailViewModel 列表的位置,以及我应该如何处理添加、删除或更改项目的顺序以保持它们同步!

How should manage the two lists (DetailModels and DetailViewModels)? I am really confused as where I initially populate the DetailViewModel list, and how I should handle adding, removing, or changing the order of the items to keep them synchronized!

推荐答案

通常 Models 只不过是数据对象.它们不应该包含任何代码来执行诸如从列表中添加/删除项目之类的操作.这是 ViewModel 的 工作.

Usually Models are nothing more than data objects. They shouldn't contain any code to do things like add/remove items from a list. This is the ViewModel's job.

在您的情况下,我将创建一个具有以下属性的 MainViewModel:

In your case, I would create a MainViewModel that has the following properties:

  • ObservableCollection详情
  • ICommand AddDetailCommand
  • ICommand RemoveDetailCommand

如果您的 MainModel 类是一个数据对象,您可以公开它,或者它也是来自 MainViewModel 的属性.公开它的属性是MVVM 纯粹主义者"的方法,而公开整个模型有时更实用.

If your MainModel class is a data object, you can either expose it, or it's properties from the MainViewModel as well. Exposing it's Properties is the "MVVM purist" approach, while exposing the entire Model is sometimes more practical.

您的MainViewModel 负责创建DetailViewModels 的初始列表,并负责添加/删除这些项目.例如,在 MainViewModel.MainModel 属性的 PropertyChanged 事件中,它可能会重建 MainViewModel.Details 集合和 CollectionChangedMainViewModel.Details 属性的 事件将更新 MainViewModel.MainModel.Details

Your MainViewModel is in charge of creating the initial list of DetailViewModels, and it is in charge of Adding/Removing these items as well. For example, in the PropertyChanged event for the MainViewModel.MainModel property, it might rebuild the MainViewModel.Details collection, and the CollectionChanged event for the MainViewModel.Details property would update MainViewModel.MainModel.Details

这篇关于如何使用集合构建 MVVM?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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