Loading and Saving Data in WPF using the MVVM pattern and DataGrids
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 开发者_JS百科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
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:
- A tutorial for Entity Framework
- A VB Centric tutorial for Entity Framework
- A little video showcasing Model-first development with Entity Framework
精彩评论