WPF 将列表绑定到多列列表框 [英] WPF Bind List to Multi-Column ListBox

查看:45
本文介绍了WPF 将列表绑定到多列列表框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从开始使用 WPF 以来,我仍然在努力理解数据绑定功能.

Since starting in WPF I am still struggling to understand the data binding functionality.

我正在尝试使用多列列表框,我的 XAMl 看起来像这样:

I am trying to use a multi column list box and my XAMl looks like this:

<ListBox Name="RecordList">
    <ListView Name="RecordListView">
        <ListView.View>
             <GridView>
                <GridView.Columns>
                     <GridViewColumn Header="1" Width="Auto" DisplayMemberBinding="{Binding Path=Field1}" />
                     <GridViewColumn Header="2" Width="50" DisplayMemberBinding="{Binding Path=Field2}" />
                     <GridViewColumn Header="3" Width="100" DisplayMemberBinding="{Binding Path=Field3}" />
                </GridView.Columns>
           </GridView>
      </ListView.View>
 </ListView>
</ListBox>

我只是无法正确使用 c# 代码来填充列表中的项目?

I just can't get the c# code right to populate the items from my list?

推荐答案

简而言之,这就是它的组合方式.

Here's the way it fits together in a nutshell.

首先,您定义一个模型来保存您的数据以进行绑定.

First, you define a model which holds your data for binding.

public sealed class MyListBoxItem
{
  public string Field1 {get;set;}
  public string Field2 {get;set;}
  public string Field3 {get;set;}
}

现在,您必须有一个类来保存这些模型以进行绑定.这种类型通常称为 ViewModel;它根据来自视图的用户输入向视图提供信息以进行绑定.它的公共属性通常是 ObservableCollections 和 DependencyProperties,因此 ViewModel 中的更改将被 View(UI)自动获取:

Now, you have to have a class that holds these models for binding. This type is often called the ViewModel; it presents information to the View for binding based on user input from the View. Its public properties are typically ObservableCollections and DependencyProperties so that changes in the ViewModel will be automatically picked up by the View (the UI):

public sealed class MyViewModel
{
  public ObservableCollection<MylistBoxItem> Items {get;private set;}
  public MyViewModel()
  {
    Items = new ObservableCollection<MyListBoxItem>();
    Items.Add(new MyListBoxItem{Field1="One", Field2="Two",Filed3="Three"};
  }
}

在 UI 的代码隐藏视图"中,实例化 ViewModel 并将其设置为视图的 DataContext.

Within the codebehind for your UI, the "View", you instantiate your ViewModel and set it as the DataContext for your View.

public MyView()
{
  this.DataContext = new MyViewModel();
}

这很重要,因为 DataContext 在可视化树中流动".它可用于设置它的每个子元素.

this is important as the DataContext "flows" through the visual tree. It is available to every child element on which it is set.

为了显示项目,我必须将 ListView 的 ItemsSource 绑定到 DataContext 的 Items 属性(这个理解).ListView 中的每一行都将其 DataContext 设置为 Items 属性中的每个单独的 MyViewModel.因此,您必须将每个显示成员绑定到 MyListBoxItem 的属性.

To display the items, I must bind the ItemsSource of the ListView to the Items property of the DataContext (this is understood). Each row within the ListView has its DataContext set to each individual MyViewModel in the Items property. So you must bind each display member to the properties of the MyListBoxItem.

<ListView Name="RecordListView" ItemsSource="{Binding Items}">
    <ListView.View>
        <GridView>
            <GridView.Columns>
                <GridViewColumn Header="1" Width="Auto" DisplayMemberBinding="{Binding Path=Field1}" />
                <GridViewColumn Header="2" Width="50" DisplayMemberBinding="{Binding Path=Field2}" />
                <GridViewColumn Header="3" Width="100" DisplayMemberBinding="{Binding Path=Field3}" />
            </GridView.Columns>
        </GridView>
    </ListView.View>
</ListView>

要更好地了解整个过程,请在此处搜索标记为 [MVVM] 的高评分问题.

To understand this whole process better, search for high-rated questions here tagged [MVVM].

另外,为了帮助调试您的绑定,请为详细数据绑定配置调试:

ALSO, to help debug your bindings, configure debugging for verbose databinding:

这篇关于WPF 将列表绑定到多列列表框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆