英文:
How to pass value from DB to Listview/Grid View via binding?
问题
我需要从数据库传递一个值到Listview通过绑定。我成功地通过简单地将值添加到列表并将它们链接到所需的列来生成静态数据。但我不明白如何使它能够显示来自我的数据库的值(我通过连接字符串和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;
}
}
如何从模型传递值:
```csharp
public partial class BuildPageAdmin : Page
{
private List<BuildModel> Builds;
public BuildPageAdmin()
{
InitializeComponent();
LVBuild.ItemsSource = BuildManager.GetBuilds();
}
}
英文:
I need to pass a value from a database to Listview via a binding. I managed to make static data by simply adding values to the List and linking them to the desired columns. But I don't understand how to make it so that I could display values from my database (I work through the connection string and MySQL).
What the markup looks like
<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>
My model
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;
}
}
How do I pass values from a model
public partial class BuildPageAdmin : Page
{
private List<BuildModel> Builds;
public BuildPageAdmin()
{
InitializeComponent();
LVBuild.ItemsSource = BuildManager.GetBuilds();
}
}
答案1
得分: 0
仅翻译代码部分:
public partial class BuildPageAdmin : Page
{
// 为 ListView 创建一个绑定源
public ObservableCollection<BuildModel> Builds { get; }
public BuildPageAdmin()
{
InitializeComponent();
this.Builds = new ObservableCollection<BuildModel>();
this.Loaded += OnPageLoaded;
}
private void OnPageLoaded(object sender, RoutedEventArgs e)
=> UpdateBuildModels();
// 随时动态更新数据源集合
private void UpdateBuildModels()
{
this.Builds.Clear();
// 从数据库获取数据
IEnumerable<BuildModel> newBuildModels = BuildManager.GetBuilds();
// 使用来自数据库的新数据更新数据源集合
foreach (BuildModel buildModel in newBuildModels)
{
this.Builds.Add(buildModel);
}
}
}
请注意,此代码是C#和XAML的代码示例,用于在WPF中使用数据绑定来动态更新数据源集合。如果您需要更多帮助,请随时提问。
英文:
Simply use data binding: create a public property to hold the source collection and update it with data from your database. Data binding overview (WPF .NET)
If your collection is a ObservableCollection
you can update it dynamically:
<Page>
<ListView ItemsSource="{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=Builds}">
</ListView>
</Page>
public partial class BuildPageAdmin : Page
{
// Create a binding source for the ListView
public ObservableCollection<BuildModel> Builds { get; }
public BuildPageAdmin()
{
InitializeComponent();
this.Builds = new ObservableCollection<BuildModel>();
this.Loaded += OnPageLoaded;
}
private void OnPageLoaded(object sender, RoutedEventArgs e)
=> UpdateBuildModels();
// Dynamically update the source collection at any time
private void UpdateBuildModels()
{
this.Builds.Clear();
// Get data from database
IEnumerable<BuldModel> newBuildModels = BuildManager.GetBuilds();
// Update source collection with new data from the database
foreach (BuildModel buildModel in newBuildModels)
{
this.Builds.Add(bulidModel);
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论