使用 MVVM 模式和 DataGrid 在 WPF 中加载和保存数据

发布于 2024-10-10 01:46:31 字数 1564 浏览 1 评论 0原文

我如何用数据填充我的 ObservableCollection(of Monitors),以便我可以以 MVVM 友好的方式保存它。我通常使用 FileNumber 使用 DataAdapter 加载数据集,并通过 MonitorDT 传递监视器表 - 我应该以不同的方式加载它吗?我如何保存更改?

现在我有以下用于加载数据的内容:

Public Class Monitors
    Inherits ObservableCollection(Of Monitor)

    Public Shared Function LoadMonitors(ByVal monitorDT As DataTable) As Monitors

        Dim monitorCollection As New Monitors

        For Each row As DataRow In monitorDT.Rows
            Dim newMonitor As New Monitor
            Dim MID As Integer = row.Item("MID")
            Dim FileNumber As String = row.Item("FileNumber")
            Dim FiscalYear As Nullable(Of Integer) = IIf(IsDBNull(row.Item("FiscalYear")), Nothing, row.Item("FiscalYear"))
            Dim Monitor As String = row.Item("Monitor")
            Dim Type As String = row.Item("Type")
            Dim MDate As Date = row.Item("MDate")
            Dim Performed As Boolean = row.Item("Performed")
            Dim Completed As Boolean = row.Item("Completed")
            Dim Comments As String = row.Item("Comments")

            newMonitor.MID = MID
            newMonitor.FileNumber = FileNumber
            newMonitor.FiscalYear = FiscalYear
            newMonitor.Monitor = Monitor
            newMonitor.Type = Type
            newMonitor.MDate = MDate
            newMonitor.Performed = Performed
            newMonitor.Completed = Completed
            newMonitor.Comments = Comments

            monitorCollection.Add(newMonitor)
        Next
        Return monitorCollection
    End Function

End Class

How do i fill my ObservableCollection(of Monitors) with data so that I can save it in an MVVM friendly manner. I normally use a FileNumber to load a DataSet using a DataAdapter and pass the monitors table via monitorDT - should I be loading it differently? How do I save the changes?

Right now I have the following for loading data:

Public Class Monitors
    Inherits ObservableCollection(Of Monitor)

    Public Shared Function LoadMonitors(ByVal monitorDT As DataTable) As Monitors

        Dim monitorCollection As New Monitors

        For Each row As DataRow In monitorDT.Rows
            Dim newMonitor As New Monitor
            Dim MID As Integer = row.Item("MID")
            Dim FileNumber As String = row.Item("FileNumber")
            Dim FiscalYear As Nullable(Of Integer) = IIf(IsDBNull(row.Item("FiscalYear")), Nothing, row.Item("FiscalYear"))
            Dim Monitor As String = row.Item("Monitor")
            Dim Type As String = row.Item("Type")
            Dim MDate As Date = row.Item("MDate")
            Dim Performed As Boolean = row.Item("Performed")
            Dim Completed As Boolean = row.Item("Completed")
            Dim Comments As String = row.Item("Comments")

            newMonitor.MID = MID
            newMonitor.FileNumber = FileNumber
            newMonitor.FiscalYear = FiscalYear
            newMonitor.Monitor = Monitor
            newMonitor.Type = Type
            newMonitor.MDate = MDate
            newMonitor.Performed = Performed
            newMonitor.Completed = Completed
            newMonitor.Comments = Comments

            monitorCollection.Add(newMonitor)
        Next
        Return monitorCollection
    End Function

End Class

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

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

发布评论

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

评论(1

↙厌世 2024-10-17 01:46:31

首先,我建议查看 ORM(对象/关系映射)框架(我更喜欢实体框架,因为它易于使用;另一个重要的框架是 NHibernate)。这将使您能够轻松地将数据库表映射到 CLR 对象。 ORM 框架提供变更跟踪和货币以及许多其他功能。

至于如何以 MVVM 友好的格式存储它,我通常有一个集合类型(在本例中)或我为其提供 ViewModel 的对象的属性集。

Public Class MonitorViewModel
    Inherits ViewModelBase

    Private _MyMonitors as Monitors
    Public Property MonitorsCollection as Monitors
        Get
            Return _MyMonitors
        End Get
        Set(ByVal value as Monitors)
            _MyMonitors = value
            OnPropertyChanged("MonitorsCollection")
        End Set
    End Property

End Class

在 ViewModel 的构造函数中,添加初始化集合所需的任何代码。

在视图中,将 ViewModel 添加为视图的 DataContext(仅当构造函数不带参数时才有效;如果有参数,则必须将 ViewModel 添加为视图 CodeBehind 中的 DataContext):

<UserControl.DataContext>
     <local.MonitorViewModel />
</UserControl.DataContext>

现在该视图下的所有控件可以访问 ViewModel。因此,如果您想要在其中显示监视器对象的 ListView,您可以这样做:

<ListBox ItemsSource="{Binding MonitorsCollection}"/>

为了节省您的时间和精力,我建议您考虑使用 MVVM 框架。它们有一些基类(如上面,我的 ViewModel 继承自 ViewModelBase),有助于加快 MVVM 开发,例如公开 INOtifyPropertyChanged、ICommand 等。我正在使用 Ocean,但还有 CaliburnMVVM Light 等等。

如果您需要任何进一步的帮助,请告诉我。

编辑:

我喜欢实体框架的一件事是,一旦创建模型(使用数据库优先或模型优先),您就可以轻松扩展实体,因为它们是部分类。例如,我通常在实体类中添加数据验证代码,以便实体验证自身,而不是让 UI 或 ViewModel 来验证。

更多链接

实体框架:

First I would recommend looking at an ORM (Object/Relational Mapping) framework (my preference is Entity Framework due to it's ease of use; another big one is NHibernate). This will allow you to do easy mapping of Database tables to your CLR Objects. ORM frameworks provide change tracking and currency and a host of other features.

As far as how to store it in an MVVM friendly format, I generally have a property set of the type of the collection (in this case) or object I am providing a ViewModel for.

Public Class MonitorViewModel
    Inherits ViewModelBase

    Private _MyMonitors as Monitors
    Public Property MonitorsCollection as Monitors
        Get
            Return _MyMonitors
        End Get
        Set(ByVal value as Monitors)
            _MyMonitors = value
            OnPropertyChanged("MonitorsCollection")
        End Set
    End Property

End Class

In your constructor for the ViewModel, add whatever code you need to initialize your collection.

In the View, add the ViewModel as the DataContext for the View (This only works if the constructor takes no arguments; if you have arguments, you have to add the ViewModel as the DataContext in the Views CodeBehind):

<UserControl.DataContext>
     <local.MonitorViewModel />
</UserControl.DataContext>

Now all controls under that View have access to the ViewModel. So if you had a ListView that you wanted to display your monitor objects in, you could just do this:

<ListBox ItemsSource="{Binding MonitorsCollection}"/>

To save yourself some time and effort, I would suggest looking into getting an MVVM framework to work with. These have some base classes (like above, my ViewModel inherits from ViewModelBase) that help expedite the MVVM development like exposing INOtifyPropertyChanged, ICommand, etc. I am using Ocean at the moment, but there is also Caliburn and MVVM Light to name a couple more.

Let me know if you need any further help.

Edit:

One of the things that I like about Entity Framework is the fact that once you create your model (Either with Database-First or Model-First) you can easily extend your entities because they are partial classes. For example, I usually add my data validation code in my entity class so that the entity validates itself, rather than having the UI or the ViewModel do it.

Some more Links

Entity Framework:

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