如何使用MVVM模式从数据库填充ListView?我更喜欢EntityFramework,但是目前我正在使用SQLAdapter进行此操作,因此我已经对工作的解决方案感到满意。
我有一个类库,称为模型。该库包含一个名为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
接下来,我有一个类库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 = "0123456",
.ProjectName = "Alpha",
.CustomerName = "AlphaCustomer"})
目标是对客户或任何列进行分组,这就是为什么我要使用ListView的原因。没关系,我已经有了第一个问题的代码,即使用MVVM从数据库中加载ListView。
我的应用程序有对ViewModels的引用。ViewModels引用了Models。
看起来像这样:
通过生成DataTable并将DataTable.DefaultView分配给ListView.ItemSource,我可以用CodeBehind填充ListView。通过使用“ DisplayMemberBinding = {Binding TableName}”选项,我可以非常快速,轻松地显示我的数据。但是我想保持它的可重用性和可维护性。=> MVVM。
MVVM有了一个好的开始。您应该了解的一件事是WPF的DataContext和DataBinding的概念。
为了实现可重用性,我通常使用View的DataBinding(可以将ViewModel绑定到控件,但我更喜欢将ViewModel用于包含控件的ViewModels的视图…)
在视图后面的代码中,我使用DataContext为视图提供上下文。ViewModel包含将被绑定的ObservableCollections。
// C#
base.DataContext = ViewModel;
// VB.Net
Me.DataContext = ViewModel;
我想你已经有了约束力
<UserControl.DataContext>
<vm:ProjectCompletedViewModel/>
</UserControl.DataContext>
完成此View-ViewModel绑定后,我可以将Binding绑定到我的ListView!
<ListView ItemsSource="{Binding MyJokers}"
[...] />
MyJokers实际上是ViewModel中我的Property的名称(请注意,该属性必须设置为Public)
public ObservableCollection<JokerViewModel> MyJokers
{
get { return _myCollection; }
private set { _myCollection = value; }
}
然后,您可以将ListViewItems绑定到此ViewModel的公共属性上(因为ListView将包含子ViewModel子级)。如果需要,可以使用ItemTemplate自定义ListViewItems。
使用此绑定,您实际上可以轻松地修改ListView和ViewModel。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句