wpf 如何通过绑定将数据库中的值传递给列表视图/网格视图?

pbossiut  于 2023-02-20  发布在  其他
关注(0)|答案(1)|浏览(194)

我需要通过绑定从数据库传递一个值到列表视图。我设法通过简单地向列表添加值并将它们链接到所需的列来生成静态数据。但我不知道如何使它能够显示来自数据库的值(我通过连接字符串和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();
        }
    }
afdcj2ne

afdcj2ne1#

只需使用数据绑定:创建一个公共属性来保存源集合,并用数据库中的数据更新它。Data binding overview (WPF .NET)
如果您的集合是ObservableCollection,您可以动态更新它:

<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);
    }
  }
}

相关问题