gpt4 book ai didi

c# - 如何使用 MVVM 模式从数据库中填充 ListView?

转载 作者:行者123 更新时间:2023-11-30 14:49:25 25 4
gpt4 key购买 nike

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

我的 View 代码如下:

<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。

我的应用程序引用了 ViewModel。 ViewModels 具有对模型的引用。

看起来像这样:

architecture0

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

最佳答案

您必须以这种方式设置 ListView:

<ListView Grid.ColumnSpan="2" HorizontalAlignment="Stretch"
VerticalAlignment="Stretch" Margin="0,52,0,0"
Grid.RowSpan="2" ItemsSource="{Binding Projects}">

ListView 的 ItemsSource 属性必须设置为 ViewModel 的公共(public)属性 Projects。现在,您可以从 View 模型中填充您的数据并填充 ObservableCollection(Of Models.ProjectCompletedModel)。

您可以删除资源:

<UserControl.Resources>
<vm:ProjectCompletedViewModel x:Key="projComplObj"/>
</UserControl.Resources>

关于c# - 如何使用 MVVM 模式从数据库中填充 ListView?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38719806/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com