如何使用MVVM模式填充数据库中的ListView?我更喜欢EntityFramework,但是目前我正在使用SQLAdapter来完成此工作,所以我已经对工作解决方案感到满意。如何使用MVVM模式填充数据库中的ListView?
我有一个类库,我称之为Models。该库包含一个名为ProjectCompletedModel.vb的类
Public Class ProjectCompletedModel
' Read this article if the Auto-Implemented Properties do not match your needs:
' https://msdn.microsoft.com/de-de/library/dd293589.aspx
''' <summary>
''' Get or set the project number.
''' </summary>
''' <value>Set the project number</value>
''' <returns>Get the project number</returns>
''' <remarks></remarks>
Public Property ProjectNumber As String
''' <summary>
''' Get or set the project name.
''' </summary>
''' <value>Set the project name</value>
''' <returns>Get the project name</returns>
''' <remarks></remarks>
Public Property ProjectName As String
''' <summary>
''' Get or set the customer name.
''' </summary>
''' <value>Set the customer name</value>
''' <returns>Get the customer name</returns>
''' <remarks></remarks>
Public Property CustomerName As String
End Class
接下来我有一个类库lib ViewModels。它包含的类ProjectCompletedViewModel.vb
Imports System.Collections.ObjectModel
Public Class ProjectCompletedViewModel
Private projCompleted As New Models.ProjectCompletedModel
Public Property Projects As ObservableCollection(Of Models.ProjectCompletedModel)
Public Property ProjectNumber As String
Get
Return Me.projCompleted.ProjectNumber
End Get
Set(value As String)
Me.projCompleted.ProjectNumber = value
End Set
End Property
Public Property ProjectName As String
Get
Return Me.projCompleted.ProjectName
End Get
Set(value As String)
Me.projCompleted.ProjectName = value
End Set
End Property
Public Property CustomerName As String
Get
Return Me.projCompleted.CustomerName
End Get
Set(value As String)
Me.projCompleted.CustomerName = value
End Set
End Property
End Class
我的观点的代码是如下因素:
<UserControl x:Class="ProjectCompletedView"
x:Name="WpfUserControl1"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:vm="clr-namespace:ViewModels;assembly=ViewModels"
mc:Ignorable="d"
d:designheight="400" d:designwidth="400" Background="#D7E4F2" d:DesignWidth="918" d:DesignHeight="478"
>
<UserControl.Resources>
<vm:ProjectCompletedViewModel x:Key="projComplObj"/>
</UserControl.Resources>
<UserControl.DataContext>
<vm:ProjectCompletedViewModel/>
</UserControl.DataContext>
<Grid HorizontalAlignment="Stretch" VerticalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="1*"/>
<ColumnDefinition Width="2*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="1*"/>
<RowDefinition Height="3*"/>
</Grid.RowDefinitions>
<ListView x:Name="lvProjects" Grid.ColumnSpan="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="0,52,0,0" Grid.RowSpan="2">
<!-- Define the column names -->
<ListView.View>
<GridView>
<GridViewColumn Header="ProjectNumber" Width="100" DisplayMemberBinding="{Binding ProjectNumber}"/>
<GridViewColumn Header="ProjectName" Width="100" DisplayMemberBinding="{Binding ProjectName}"/>
<GridViewColumn Header="Customer" Width="100" DisplayMemberBinding="{Binding CustomerName}"/>
</GridView>
</ListView.View>
</ListView>
</Grid>
</UserControl>
这导致由.xaml.vb的构造函数中添加一些硬编码值以下的设计文件。
Me.lvProjects.Items.Add(New ViewModels.ProjectCompletedViewModel() With {.ProjectNumber = "",
.ProjectName = "Alpha",
.CustomerName = "AlphaCustomer"})
的目标是集团客户或者这就是为什么我想用一个ListView工作的任何列。没关系分组我已经有了代码,我的第一个问题是从DataBase加载MVVM的ListView。
我的应用程序引用了ViewModels。 ViewModels有一个对Models的引用。
是这样的:
我能够通过生成一个DataTable和分配DataTable.DefaultView到ListView.ItemSource以填补代码隐藏ListView控件。通过使用“DisplayMemberBinding = {Binding TableName}”选项,我可以非常快速简单地显示数据。但我想保持它的可重用性和可维护性。 => MVVM。
在这个人的帮助下这里http://stackoverflow.com/questions/14179967/fill-observablecollection-directly-from-sql-server你的指示使我解决了这个问题。谢谢!您可以在VB.NET中轻松调整它。只需将base.DataContext =“..”更改为Me.DataContext = New ViewModel。 –
谢谢,我会更新我的答案以结合上下文绑定。 –