如何通过绑定从数据库传递值到ListView/GridView?

huangapple go评论99阅读模式
英文:

How to pass value from DB to Listview/Grid View via binding?

问题

我需要从数据库传递一个值到Listview通过绑定。我成功地通过简单地将值添加到列表并将它们链接到所需的列来生成静态数据。但我不明白如何使它能够显示来自我的数据库的值(我通过连接字符串和MySQL工作)。

标记的外观如下所示:

  1. <ListView Name="LVBuild">
  2. <ListView.View>
  3. <GridView>
  4. <GridViewColumn DisplayMemberBinding="{Binding ID}" Header="ID" />
  5. <GridViewColumn DisplayMemberBinding="{Binding Title}" Header="Title" />
  6. <GridViewColumn DisplayMemberBinding="{Binding Description}" Header="Description" />
  7. <GridViewColumn DisplayMemberBinding="{Binding BuildData}" Header="BuildDate">
  8. <GridViewColumn.CellTemplate>

我的模型:

  1. public class BuildModel
  2. {
  3. public int ID { get; set; }
  4. public string Title { get; set; }
  5. public string Description { get; set; }
  6. public string BuildData { get; set; }
  7. public int Architect { get; set; }
  8. public int Location { get; set; }
  9. public int Teg { get; set; }
  10. }
  11. public class BuildManager
  12. {
  13. public static List<BuildModel> GetBuilds()
  14. {
  15. List<BuildModel> items = new List<BuildModel>();
  16. items.Add(new BuildModel() { ID = 1, Title = "Test1", Description = "Desc1", BuildData = "12.12.2022", Architect = 1, Location = 1, Teg = 1 });
  17. items.Add(new BuildModel() { ID = 2, Title = "Test2", Description = "Desc2" });
  18. items.Add(new BuildModel() { ID = 3, Title = "Test3" });
  19. items.Add(new BuildModel() { ID = 4, Title = "Test4" });
  20. return items;
  21. }
  22. }
  23. 如何从模型传递值:
  24. ```csharp
  25. public partial class BuildPageAdmin : Page
  26. {
  27. private List<BuildModel> Builds;
  28. public BuildPageAdmin()
  29. {
  30. InitializeComponent();
  31. LVBuild.ItemsSource = BuildManager.GetBuilds();
  32. }
  33. }
英文:

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

  1. &lt;ListView
  2. Name=&quot;LVBuild&quot;&gt;
  3. &lt;ListView.View&gt;
  4. &lt;GridView&gt;
  5. &lt;GridViewColumn
  6. DisplayMemberBinding=&quot;{Binding ID}&quot;
  7. Header=&quot;ID&quot; /&gt;
  8. &lt;GridViewColumn
  9. DisplayMemberBinding=&quot;{Binding Title}&quot;
  10. Header=&quot;Title&quot; /&gt;
  11. &lt;GridViewColumn
  12. DisplayMemberBinding=&quot;{Binding Description}&quot;
  13. Header=&quot;Description&quot; /&gt;
  14. &lt;GridViewColumn
  15. DisplayMemberBinding=&quot;{Binding BuildData}&quot;
  16. Header=&quot;BuildDate&quot;&gt;
  17. &lt;GridViewColumn.CellTemplate&gt;

My model

  1. public class BuildModel
  2. {
  3. public int ID { get; set; }
  4. public string Title { get; set; }
  5. public string Description { get; set; }
  6. public string BuildData { get; set; }
  7. public int Architect { get; set; }
  8. public int Location { get; set; }
  9. public int Teg { get; set; }
  10. }
  11. public class BuildManager
  12. {
  13. public static List&lt;BuildModel&gt; GetBuilds()
  14. {
  15. List&lt;BuildModel&gt; items = new List&lt;BuildModel&gt;();
  16. items.Add(new BuildModel() {ID = 1, Title = &quot;Test1&quot;, Description = &quot;Desc1&quot;, BuildData = &quot;12.12.2022&quot;, Architect = 1, Location = 1, Teg = 1});
  17. items.Add(new BuildModel() {ID = 2, Title = &quot;Test2&quot;, Description = &quot;Desc2&quot;});
  18. items.Add(new BuildModel() {ID = 3, Title = &quot;Test3&quot;});
  19. items.Add(new BuildModel() {ID = 4, Title = &quot;Test4&quot;});
  20. return items;
  21. }
  22. }

How do I pass values from a model

  1. public partial class BuildPageAdmin : Page
  2. {
  3. private List&lt;BuildModel&gt; Builds;
  4. public BuildPageAdmin()
  5. {
  6. InitializeComponent();
  7. LVBuild.ItemsSource = BuildManager.GetBuilds();
  8. }
  9. }

答案1

得分: 0

仅翻译代码部分:

  1. public partial class BuildPageAdmin : Page
  2. {
  3. // 为 ListView 创建一个绑定源
  4. public ObservableCollection<BuildModel> Builds { get; }
  5. public BuildPageAdmin()
  6. {
  7. InitializeComponent();
  8. this.Builds = new ObservableCollection<BuildModel>();
  9. this.Loaded += OnPageLoaded;
  10. }
  11. private void OnPageLoaded(object sender, RoutedEventArgs e)
  12. => UpdateBuildModels();
  13. // 随时动态更新数据源集合
  14. private void UpdateBuildModels()
  15. {
  16. this.Builds.Clear();
  17. // 从数据库获取数据
  18. IEnumerable<BuildModel> newBuildModels = BuildManager.GetBuilds();
  19. // 使用来自数据库的新数据更新数据源集合
  20. foreach (BuildModel buildModel in newBuildModels)
  21. {
  22. this.Builds.Add(buildModel);
  23. }
  24. }
  25. }

请注意,此代码是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:

  1. &lt;Page&gt;
  2. &lt;ListView ItemsSource=&quot;{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=Builds}&quot;&gt;
  3. &lt;/ListView&gt;
  4. &lt;/Page&gt;
  1. public partial class BuildPageAdmin : Page
  2. {
  3. // Create a binding source for the ListView
  4. public ObservableCollection&lt;BuildModel&gt; Builds { get; }
  5. public BuildPageAdmin()
  6. {
  7. InitializeComponent();
  8. this.Builds = new ObservableCollection&lt;BuildModel&gt;();
  9. this.Loaded += OnPageLoaded;
  10. }
  11. private void OnPageLoaded(object sender, RoutedEventArgs e)
  12. =&gt; UpdateBuildModels();
  13. // Dynamically update the source collection at any time
  14. private void UpdateBuildModels()
  15. {
  16. this.Builds.Clear();
  17. // Get data from database
  18. IEnumerable&lt;BuldModel&gt; newBuildModels = BuildManager.GetBuilds();
  19. // Update source collection with new data from the database
  20. foreach (BuildModel buildModel in newBuildModels)
  21. {
  22. this.Builds.Add(bulidModel);
  23. }
  24. }
  25. }

huangapple
  • 本文由 发表于 2023年2月19日 19:38:38
  • 转载请务必保留本文链接:https://go.coder-hub.com/75499858.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定