我需要通过绑定从数据库传递一个值到列表视图。我设法通过简单地向列表添加值并将它们链接到所需的列来生成静态数据。但我不知道如何使它能够显示来自数据库的值(我通过连接字符串和MySQL工作)。
标记的外观
<ListView
Name="LVBuild">
<ListView.View>
<GridView>
<GridViewColumn
DisplayMemberBinding="{Binding ID}"
Header="ID" />
<GridViewColumn
DisplayMemberBinding="{Binding Title}"
Header="Title" />
<GridViewColumn
DisplayMemberBinding="{Binding Description}"
Header="Description" />
<GridViewColumn
DisplayMemberBinding="{Binding BuildData}"
Header="BuildDate">
<GridViewColumn.CellTemplate>
我的模特
public class BuildModel
{
public int ID { get; set; }
public string Title { get; set; }
public string Description { get; set; }
public string BuildData { get; set; }
public int Architect { get; set; }
public int Location { get; set; }
public int Teg { get; set; }
}
public class BuildManager
{
public static List<BuildModel> GetBuilds()
{
List<BuildModel> items = new List<BuildModel>();
items.Add(new BuildModel() {ID = 1, Title = "Test1", Description = "Desc1", BuildData = "12.12.2022", Architect = 1, Location = 1, Teg = 1});
items.Add(new BuildModel() {ID = 2, Title = "Test2", Description = "Desc2"});
items.Add(new BuildModel() {ID = 3, Title = "Test3"});
items.Add(new BuildModel() {ID = 4, Title = "Test4"});
return items;
}
}
如何从模型传递值
public partial class BuildPageAdmin : Page
{
private List<BuildModel> Builds;
public BuildPageAdmin()
{
InitializeComponent();
LVBuild.ItemsSource = BuildManager.GetBuilds();
}
}
1条答案
按热度按时间afdcj2ne1#
只需使用数据绑定:创建一个公共属性来保存源集合,并用数据库中的数据更新它。Data binding overview (WPF .NET)
如果您的集合是
ObservableCollection
,您可以动态更新它: