C# 如何使用MVVM模式从数据库填充ListView?
如何使用MVVM模式从数据库填充ListView?我更喜欢EntityFramework,但目前我正在使用SQLAdapter来实现这一点,因此我已经对一个可行的解决方案感到满意了 我有一个类库,我称之为模型。此库包含一个名为ProjectCompletedModel.vb的类C# 如何使用MVVM模式从数据库填充ListView?,c#,.net,wpf,vb.net,mvvm,C#,.net,Wpf,Vb.net,Mvvm,如何使用MVVM模式从数据库填充ListView?我更喜欢EntityFramework,但目前我正在使用SQLAdapter来实现这一点,因此我已经对一个可行的解决方案感到满意了 我有一个类库,我称之为模型。此库包含一个名为ProjectCompletedModel.vb的类 Public Class ProjectCompletedModel ' Read this article if the Auto-Implemented Properties do not match yo
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
我的观点代码如下:
<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>
目标是对客户或任何专栏进行分组,这就是我希望使用ListView的原因。不用管分组了,我已经有了代码,我的第一个问题是用MVVM从数据库加载ListView
我的应用程序引用了ViewModels。ViewModels具有对模型的引用
看起来像这样:
通过生成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的公共属性项目。现在,从viewmodel,您可以用数据填充和填充ObservableCollection(Of Models.ProjectCompletedModel)
您可以删除以下资源:
<UserControl.Resources>
<vm:ProjectCompletedViewModel x:Key="projComplObj"/>
</UserControl.Resources>
MVVM有了一个良好的开端。您应该了解的一件事是WPF的DataContext和数据绑定的概念 为了可重用性,我通常使用视图的数据绑定(您可以将ViewModel绑定到控件,但我更喜欢对包含控件ViewModels的视图使用ViewModel…) 在视图的代码隐藏中,我使用DataContext为视图提供上下文。ViewModel包含将被绑定的ObservableCollections
// C#
base.DataContext = ViewModel;
// VB.Net
Me.DataContext = ViewModel;
我想你已经有了一本书了
<UserControl.DataContext>
<vm:ProjectCompletedViewModel/>
</UserControl.DataContext>
在这个视图视图模型绑定之后,我可以给我的ListView提供一个绑定
<ListView ItemsSource="{Binding MyJokers}"
[...] />
MyJokers实际上是我的ViewModel中属性的名称(请注意,属性必须设置为Public)
公共可观察收集MyJokers
{
获取{return\u myCollection;}
私有集{u myCollection=value;}
}
然后,可以在此ViewModel的公共属性上绑定ListViewItems(因为ListView将包含子ViewModel子项)。如果需要,可以使用ItemTemplate自定义ListViewItems
使用此绑定,您实际上可以轻松地修改ListView和ViewModel 谢谢您的快速回复。我会尽快处理并报告你。我已经删除了UserControl.Resources,它没有破坏我的应用程序。我感兴趣的是为什么我可以在不破坏任何东西的情况下移除它。谢谢。您不需要XAML中的任何资源,因为您已经在视图中添加了datacontext,您可以在资源中添加viewmodel,也可以删除块。您必须这样设置外部网格:。您帮助我解决了一些问题。谢谢但按照艾蒂安的指示,它起了作用。但是非常感谢你帮了我很多。在这个家伙的帮助下,你的指示让我解决了这个问题。谢谢您可以在VB.NET中轻松地进行调整。只需将base.DataContext=“..”更改为Me.DataContext=New ViewModel。谢谢,我将更新我的答案以合并上下文绑定。
<UserControl.DataContext>
<vm:ProjectCompletedViewModel/>
</UserControl.DataContext>
<ListView ItemsSource="{Binding MyJokers}"
[...] />
public ObservableCollection<JokerViewModel> MyJokers
{
get { return _myCollection; }
private set { _myCollection = value; }
}