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

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

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

  &lt;ListView
                    Name=&quot;LVBuild&quot;&gt;
                    &lt;ListView.View&gt;
                    &lt;GridView&gt;
                        &lt;GridViewColumn 
                            DisplayMemberBinding=&quot;{Binding  ID}&quot;
                            Header=&quot;ID&quot; /&gt;
                        &lt;GridViewColumn
                            DisplayMemberBinding=&quot;{Binding Title}&quot;
                            Header=&quot;Title&quot; /&gt;
                        &lt;GridViewColumn
                            DisplayMemberBinding=&quot;{Binding Description}&quot;
                            Header=&quot;Description&quot; /&gt;
                        &lt;GridViewColumn
                            DisplayMemberBinding=&quot;{Binding BuildData}&quot;
                            Header=&quot;BuildDate&quot;&gt;
                            &lt;GridViewColumn.CellTemplate&gt;

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&lt;BuildModel&gt; GetBuilds()
        {
            List&lt;BuildModel&gt; items = new List&lt;BuildModel&gt;();
            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});
            items.Add(new BuildModel() {ID = 2, Title = &quot;Test2&quot;, Description = &quot;Desc2&quot;});
            items.Add(new BuildModel() {ID = 3, Title = &quot;Test3&quot;});
            items.Add(new BuildModel() {ID = 4, Title = &quot;Test4&quot;});
            return items;
        }
        
    }

How do I pass values from a model

public partial class BuildPageAdmin : Page
    {

        private List&lt;BuildModel&gt; 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:

&lt;Page&gt;
  &lt;ListView ItemsSource=&quot;{Binding RelativeSource={RelativeSource AncestorType=Page}, Path=Builds}&quot;&gt;

  &lt;/ListView&gt;
&lt;/Page&gt;
public partial class BuildPageAdmin : Page
{
  // Create a binding source for the ListView
  public ObservableCollection&lt;BuildModel&gt; Builds { get; }

  public BuildPageAdmin()
  {
    InitializeComponent();
 
    this.Builds = new ObservableCollection&lt;BuildModel&gt;();
    this.Loaded += OnPageLoaded;
  }

  private void OnPageLoaded(object sender, RoutedEventArgs e) 
    =&gt; UpdateBuildModels();

  // Dynamically update the source collection at any time
  private void UpdateBuildModels()
  {
    this.Builds.Clear();

    // Get data from database
    IEnumerable&lt;BuldModel&gt; newBuildModels = BuildManager.GetBuilds();

    // Update source collection with new data from the database
    foreach (BuildModel buildModel in newBuildModels)
    {
      this.Builds.Add(bulidModel);
    }
  }
}

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:

确定