我的 ViewModel 应该有视图或 ViewModel 的 ObservableCollection 吗?
我试图通过通过 DataTemplates 将其绑定到 ViewModel 上的 ObservableCollections 来了解使用 ItemsControl 时的基本 MVVM 设计方法。
我见过绑定到字符串、视图和ViewModels的ObservableCollections的示例。
绑定到字符串似乎只用于演示,它是绑定到“包含 ViewModels 集合的 ViewModels 包含 ViewModels 集合的集合”,WPF 的强大功能似乎确实如此。出来。
对于那些精通 MVVM 模式的人来说,将 ItemsControl、ListView、ListBox 绑定到 ViewModel 中的集合的标准方法是什么?我正在寻找这样的经验建议:
- 始终使用 ObservableCollection ...> 并且从不列出<...> 因为...
- 比 ItemsControl 更好地显示集合的是...
- 为了让过滤在 ViewModel 中工作而不是在代码隐藏中工作,请使用...
- 使用视图集合,当...和 ViewModel 集合时。 ..
- 90% 的时间我创建一个 ItemsControl 并将其绑定到具有自己的 ViewModel 的视图的 ObservableCollection...
I'm trying to understand the basic MVVM design approach when using ItemsControl by binding it via DataTemplates to ObservableCollections on the ViewModel.
I've seen examples that bind to ObservableCollections of strings, Views, and ViewModels.
Binding to strings seems to be only for demos, it is the binding to "ViewModels that contain collections of Views that contain collections of ViewModels" that the power of WPF seems to really come out.
For those of use proficient in the MVVM pattern, what is your standard approach to binding ItemsControl, ListView, ListBox to collections in a ViewModel? I'm looking for advice from experience like this:
- always use ObservableCollection<...> and never List<...> because...
- something better than ItemsControl to display a collection is...
- in order to get filtering to work in your ViewModel instead of code-behind, use...
- use collections of Views when ... and collections of ViewModels when...
- 90% of the time I create an ItemsControl and bind it to an ObservableCollection of Views which have their own ViewModels...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我会使用 ViewModel 的 ObservableCollection ,原因如下:
I would use an ObservableCollection of ViewModels for the following reasons:
我喜欢使用 ViewModel 的 ObservableCollection。 绑定到集合的视图可以定义一个 DataTemplate,为 ViewModel 提供其外观。 这会减少组件之间的耦合。
I like using an ObservableCollection of ViewModels. The view that binds to the collection can define a DataTemplate that gives the ViewModel its look. This leads to less coupling among the components.
我有同样的问题,但将“视图”替换为“模型”。 :)
我有一个模型和其他模型的集合。
我希望我的视图模型具有其他视图模型的可观察集合,但是一旦我像这样实例化它 - 模型集合内容之间的连接就会丢失。
我现在是否需要开始将所有事件从 viewmodels 可观察集合连接回模型集合?
I have the same question, but replace the "view" with "model". :)
I have a MODEL with a collection of other models.
I want my viewmodel to have an observable collection of other viewmodels, but once I instantiate it like that - the connection between the model collection content is lost.
Do I now need to start wiring all the events from the viewmodels observable collection back to the models collection?