【问题标题】:C# WPF binding collection to the listviewC# WPF 将集合绑定到列表视图
【发布时间】:2016-02-24 13:23:41
【问题描述】:

我是 WPF 中数据绑定的新手,并试图理解概念。我要做的是将可观察的集合数据绑定到列表视图。为此,我创建了两个类:

public class Employee_list
    {
        public ObservableCollection<Employee> list = new ObservableCollection <Employee>();
        public Employee_list()
        {
        }
    }

    public class Employee
    {
        public string name { get; set; }
        public string surname { get; set;}
        public Employee(string name, string surname)
        {
            this.name = name;
            this.surname = surname;
        }
    }

在主窗口中,我实例化了我的员工列表:

    public MainWindow()
    {
        InitializeComponent();
        Employee_list l = new Employee_list() { list = { new Employee("Alex", "Z"), new Employee ("Alex", "K")}};            
    }

现在我需要将此列表的内容绑定到 ListView:

我知道我可以通过三种方式做到这一点 - 1

  1. RelativeSource:相对源是绑定上的属性之一,它可以指向相对源标记扩展,该扩展指示可以在层次结构中找到源的位置。简单来说就是元素层次结构中的相对路径。

  2. ElementName:另一种指定源的方法是使用 ElementName,其中当前 UI 中存在可用作源对象的另一个元素。这里源对象必须是可视化树的一部分。

  3. Source:另一种方法是在绑定上使用 Source 属性。此源属性必须指向某个对象引用,并且将对象引用向下传递到 Source 属性的唯一更好的方法是使用指向资源集合中某个对象的静态资源。

我尝试通过 Source 和 Resources 来实现,但在这种情况下,我可以指定类本身,而不是指定包含内容的特定集合:

<Window.Resources>
    <localc:Employee_list x:Key="EmployeeList" />
</Window.Resources>

您能否帮助我了解我该怎么做以及正确的方法是什么?

XAML:

<Window x:Name="mw" 
x:Class="binding_testing.MainWindow"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:localc="clr-namespace:binding_testing"
    Title="MainWindow" Height="350" Width="525">
<Window.Resources>
    <localc:Employee_list x:Key="EmployeeList" />
</Window.Resources>
<Grid>
    <ListView Name="myListBox" HorizontalAlignment="Left" Height="89" Margin="75,77,0,0" VerticalAlignment="Top" Width="393"
         ItemsSource="{Binding Source={StaticResource EmployeeList}}" >
        <ListView.View>
            <GridView >
                <GridViewColumn Header="Surname" Width="Auto" DisplayMemberBinding="{Binding surname}" />
                <GridViewColumn Header="Name" Width="Auto" DisplayMemberBinding="{Binding name}" />
            </GridView>
        </ListView.View>
    </ListView>

</Grid>
</Window>

【问题讨论】:

    标签: c# wpf xaml listview data-binding


    【解决方案1】:

    进行以下更改:

    1. 将您的集合公开为属性,因为 DataBinding 使用属性而不是字段。

      public class Employee_list
      { 
          ObservableCollection<Employee> list = new ObservableCollection <Employee>();
      
          public ObservableCollection<Employee> List1
          {
              get { return list; }
          }
      
          public Employee_list()
          {
              List1.Add(new Employee("Alex", "Z"));
              List1.Add(new Employee("Alex", "K"));
          }
      }
      
    2. 而不是 ItemsSource,绑定 DataContext,然后使用您的 Collection 属性作为 ItemsSource,例如:

      <ListView ... 
           DataContext="{Binding Source={StaticResource EmployeeList}}" ItemsSource="{Binding List1}" >
      

    很少有好的链接:

    1. DataBinding - How to
    2. DataBinding - FAQ
    3. Scott's DataBinding tutorial

    【讨论】:

      【解决方案2】:

      所以请先明确您的ClassesHere you find the Microsoft Naming Guidelines.

      我将你的类和属性重命名为:

      public class EmployeeList
      {
          public ObservableCollection<Employee> List { get; set; } = new ObservableCollection<Employee>();
      }
      
      public class Employee
      {
          public string Name { get; set; }
          public string SurName { get; set; }
          public Employee(string name, string surName)
          {
              this.Name = name;
              this.SurName = surName;
          }
      }
      

      确保所有Properties 都是public 并且也是properties。因为List 只是您代码中的一个字段

      那我推荐介绍View这个类。这也是MVVM pattern 的概念,但我认为它使代码更容易,所以这在两个方面都很好:

      public class View
      {
          public EmployeeList EmployeeList { get; set; }
      
          public View()
          {
              EmployeeList = new EmployeeList() { List = { new Employee("Alex", "Z"), new Employee("Alex", "K") } };
          }
      }
      

      如您所见,View 拥有EmployeeList 的一个实例。


      现在您只需将窗口的 DataContext 设置为 View 并绑定到您想要的属性:

      public partial class MainWindow : Window
      {
          public MainWindow()
          {
              InitializeComponent();
              DataContext = new View();
          }
      }
      

      上面的代码创建了View 类的一个新实例,并将其设置为新的DataContext

      现在您只需要将ListViewItemsSource 绑定到EmployeeList.List 并设置DisplayMemeberBindings:

      <Grid>
          <ListView ItemsSource="{Binding EmployeeList.List}" >
              <ListView.View>
                  <GridView >
                      <GridViewColumn Header="Surname" DisplayMemberBinding="{Binding SurName}" />
                      <GridViewColumn Header="Name" DisplayMemberBinding="{Binding Name}" />
                  </GridView>
              </ListView.View>
          </ListView>
      </Grid>
      

      你的代码的问题是你有两个EmployeeList的实例

      1. 在 XAML 中定义
      2. 在窗口的构造函数中创建

      此外,您无法绑定到ObservableCollection,因为它不是Property。您只能绑定到属性。无法绑定到字段。所以要明确你有一个get {...} 或/和set {...} 来获取你想要的属性。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-09-22
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-25
        • 2012-02-28
        • 2012-02-08
        相关资源
        最近更新 更多