如何使用嵌套的observablecollections构造视图模型和模型 [英] How to structure viewmodels and models with nested observablecollections
问题描述
我正在努力解决这个问题,因为我无法找到一个好的例子或充分的讨论。我很难理解如何构建具有多个嵌套集合的WPF MVVM应用程序。
该应用程序将从SQL2000数据库查看技术支持票证。门票集合包含事件,升级和RMA的单独集合。一张票可以与零个或多个事件,升级或RMA相关。为了使它更复杂一点,我不只是用数据库中的内容显示集合,我还提供了我想在DataGrid中显示的摘要和总属性。
我有 MainWindow
查看(窗口), TicketsView
(UserControl)和 TicketView
(UserControl)。它们分别对应于 MainWindowViewModel
, TicketsViewModel
和 TicketViewModel
。然后是 Ticket
, Event
, Escalation
和 Rma
模型类;以及与数据库交互的 TicketRepository
类。我应该在哪里找到ObservableCollections,这些集合应该是什么?我可以创建一个 ObservableCollection< Ticket>
;在 TicketsViewModel
中 TicketsView.TicketsDataGrid
绑定到;当 SelectedItem
通过 Ticket
时,只需创建 TicketViewModel
宾语。但是,这不允许我在DataGrid中拥有计算/总属性。
任何帮助或指导都将不胜感激。
我尝试了什么:
我已经尝试使用Model对象创建了ObservableCollections在SelectedItem传递Model类之后创建ViewModel对象。如果我没有任何摘要/总属性,那么效果很好;如果我遵循更为流行的观点,即将这些属性保留在您的Model类之外。
然后我为任何需要这些类型属性的东西创建了ViewModels,而不仅仅是传递对象时创建它们。这变得非常复杂,看起来可能不对。我可以创建所有 TicketViewModel
项目;因为有一个相应的 TicketView
UserControl。我没有与 EventViewModel
, EscalationViewModel
或 RmaViewModel $ c相对应的视图$ C>;因为它们只显示在
EventsDataGrid
, EscalationsDataGrid
和 RmasDataGrid
里面 TicketView
。或者可以吗?或者,我是否应该为那些具有属性和Model对象的集合提供其他类型的自定义集合或包装器以及我正在公开的属性?
确定去提前!
这很好,甚至人们做什么
类 MyViewModel:ModelBase
{
public AModel MyProperty
{
get { return mProperty; }
set {
mProperty = value ;
OnPropertyChanged();
}
}
public ObservableCollection< othermodel>儿童{获取; } = ObservableCollection< othermodel>
();
} < / othermodel > < / othermodel >
实际上你做任何你想做的事!
唯一要记住的是这些 ViewModels是视图的模型。因此,它们的(唯一)约束如下:* 如果必须在视图中反映更改,则INotifyPropertyChange
或INotifyCollectionChanged
(适当的)必须用它来解雇 *。
它恰好发生在
< span class =code-keyword> class ObservableCollection< othermodel> :INotifyCollectionChanged < / othermodel >
I’m struggling with this concept, as I haven’t been able to find a good example or ample discussion. I’m having a hard time understanding how to structure a WPF MVVM app that has multiple nested collections.
The application is to view Technical Support Tickets from an SQL2000 database. The collection of Tickets has separate collections of Events, Escalations, and RMAs. One Ticket can relate to zero or more Events, Escalations, or RMAs. To make it a bit more complicated, I’m not just displaying the collections with what I pull from the database, I’m also providing summary and total properties that I want to display in the DataGrid.
I have MainWindow
View (Window), TicketsView
(UserControl), and TicketView
(UserControl). Those correspond to MainWindowViewModel
, TicketsViewModel
, and TicketViewModel
respectively. Then there’s Ticket
, Event
, Escalation
, and Rma
Model classes; and a TicketRepository
class that interacts with the database. Where should I locate the ObservableCollections, and what should those collections be? I can create an ObservableCollection<Ticket>
; in TicketsViewModel
that the TicketsView.TicketsDataGrid
binds to; and just create the TicketViewModel
when the SelectedItem
passes the Ticket
object. However that doesn’t allow me to have the calculation/total properties in the DataGrid.
Any assistance or guidance would be appreciated.
What I have tried:
I’ve tried already creating the ObservableCollections with just the Model object and creating the ViewModel object after SelectedItem passes the Model class. Works well, if I don’t have any summary/total properties; and if I follow the more popular opinion of keeping those properties out of your Model classes.
I then created ViewModels for anything that required those types of properties, instead of just creating them when passed the object. This gets really complicated fast and seems like it may not be right. I’m okay creating all the TicketViewModel
items; as there is a corresponding TicketView
UserControl. I don’t have Views corresponding to EventViewModel
, EscalationViewModel
, or RmaViewModel
; as they’re only showing in the EventsDataGrid
, EscalationsDataGrid
, and RmasDataGrid
inside TicketView
. Or is that okay? Or, should I have some other sort of custom collection or wrapper for those collections that has the properties and Model object with the properties I’m exposing?
sure go ahead!
this is fine, and even what people do
class MyViewModel : ModelBase { public AModel MyProperty { get { return mProperty; } set { mProperty = value; OnPropertyChanged(); } } public ObservableCollection<othermodel> Children { get; } = ObservableCollection<othermodel> (); }</othermodel></othermodel>
In fact you do whatever you want!
The only one thing to remember is that these "ViewModels" are models for the views. As such their (only) constraint is the following: *if a change must be reflected in the view, anINotifyPropertyChange
orINotifyCollectionChanged
(which ever appropriate) must be fired with it*.
It just happen that
class ObservableCollection<othermodel> : INotifyCollectionChanged</othermodel>
这篇关于如何使用嵌套的observablecollections构造视图模型和模型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!