如何使用MVVM模式从数据库填充ListView?

Matthis Kohli

如何使用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"})

GUI0

目标是对客户或任何列进行分组,这就是为什么我要使用ListView的原因。没关系,我已经有了第一个问题的代码,即使用MVVM从数据库中加载ListView。

我的应用程序有对ViewModels的引用。ViewModels引用了Models。

看起来像这样:

建筑0

通过生成DataTable并将DataTable.DefaultView分配给ListView.ItemSource,我可以用CodeBehind填充ListView。通过使用“ DisplayMemberBinding = {Binding TableName}”选项,我可以非常快速,轻松地显示我的数据。但是我想保持它的可重用性和可维护性。=> MVVM。

艾蒂安·福彻(Etienne Faucher)

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] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何用片段内的数据库数据填充listview?

如何使用 ImageView 将现有 sqlite 数据库中的数据填充到 ListView

如何使用资产文件夹中的数据库填充ListView?

无法使用 SQLite 数据库查询填充 ListView

从Firebase数据库填充ListView数据

尝试从SQLite数据库填充ListView

如何使用python faker填充数据库

如何使用 Firebase 数据库填充对象数组

我如何使用函数isClicked()填充数据库?

如何使用MVC从数据库表填充下拉列表?

如何使用数据库中的记录填充组合框

如何使用SQLite数据库数据库在地图上填充动态标记?

Ruby on Rails-使用数据库中的数据填充模式吗?

如何通过MVVM模式中的小部件与数据库进行交互

如何从 Firebase 数据库获取数据并将其填充到 ListView 中

Firebase 数据库使用 FirebaseUI 将子数据填充到 ListView

如何通过不同的数据库模式使用JPQL?

当使用模式数据库时,sails.js如何处理数据库迁移?

MVVM模式一定需要数据库吗?

无法从Android中的数据库填充listView中的数据

如何从SQLite数据库填充recyclerview

如何从数据库填充导航栏

如何从数据库填充WPF窗口

如何从数据库填充DropdownlistFor控件?

Android:使用数据库填充Listview,崩溃且不显示

使用CoreData数据库填充NSMutableArray

使用Firebase数据库填充RecyclerView

从Android上的SQLite数据库填充ListView

从 SQLite 数据库填充 ListView 不起作用