如何在 ViewModel 中公开模型集合
我正在为一个表示递归嵌套类型的类实现 MVVM。例如:
class NestedType
{
// Other properties here
public Collection<NestedType> SubElements {get; set;}
}
class NestedTypeViewModel
{
ObservableCollection<NestedType> ModelCollection { ??? }
}
如何以可观察的方式公开模型集合中的项目? (即,视图将添加、创建和修改子元素)我假设我需要 ViewModel 中的集合为 ObservableCollection
但在 Model 中呢?我还可以将其设为 ObservableCollection 并直接公开它...
有什么建议吗?
I'm implementing MVVM for a class which represents recursively nested types. For example:
class NestedType
{
// Other properties here
public Collection<NestedType> SubElements {get; set;}
}
class NestedTypeViewModel
{
ObservableCollection<NestedType> ModelCollection { ??? }
}
How do I expose the items in the Model's collection in an observable way? (i.e., the View will be adding, creating, and modifying the subelements) I assume I need the collection in the ViewModel to be ObservableCollection<T>
but what about in the Model? I could also make that an ObservableCollection
and just directly expose it...
Any suggestions?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以直接从模型返回 ObservableList。 WPF 将获取 Collection 的引用并注册到模型集合的 CollectionChanged 事件。
但是,您将破坏使用 MVVM 想要实现的目标。 ViewModel 不应公开模型、单个模型或集合,否则您的视图可能会绑定到模型,而您不应该在 MVVM 中这样做。
MVVM 不是宗教,每个人以不同的方式实现某些部分。如果这对你来说没问题……那就直接做吧。
如果您想要一个干净的解决方案,您有多种选择。只需允许管理器修改模型的集合并创建相应的视图模型,或者我最喜欢的方法是拥有一个同步模型和视图模型集合的对象,通过自动将新添加的模型包装在新的视图模型中并将此虚拟机添加到父视图模型列表。
You can directly return the ObservableList from your model. WPF will take the reference of the Collection and registers to the CollectionChanged Event of the model collection.
BUT, you will break what you are trying to achieve by using MVVM. A ViewModel should not expose a model, single or collection, otherwise your view could bind to a model which you should not do in MVVM.
Well MVVM is no religion and everyone implements certain parts differently. If this is no problem for you ... Go ahead and do it directly.
If you want a clean solution, you have multiple options to go from here. Just allowing a Manager to modify the collection of the model and create a corresponding view model or My favorit approach is to have an object that syncs model and viewmodel collection, by automatically wrap newly added models in a new view model and add this vm into the parent viewmodel list.
只要您不需要将不同的
Models
传递到ViewModel
中,您就可以没有单独的Model
实体,因此您可以坚持使用>ViewModel
代表实际的Model
数据。我建议在引擎盖下使用私有字段以及
INotifyPropertyChanged
来公开它:编辑:评论的答案
但是如果您已经拥有像 Model 这样的实体 - 那么请尝试注入ViewModel 通过接口,因此 Model 和 ViewModel 至少会解耦
You can live without separate
Model
entity as long as you don't need to pass differentModels
intoViewModel
, so you can stick withViewModel
which represents an actualModel
data.I would recommend to expose it using private field under the hood along with
INotifyPropertyChanged
:EDIT: answer to comments
But if you already has such entity like Model - so try to inject in ViewModel by an Interface so Model and ViewModel at least would be decoupled