C# 为什么不是';t我的标签通过组合框更新

C# 为什么不是';t我的标签通过组合框更新,c#,winforms,data-binding,C#,Winforms,Data Binding,我正在使用从我的客户对象到组合框的数据绑定。我试图实现的行为是,标签文本将反映所选的名称 using System; using System.Collections.Generic; using System.Windows.Forms; namespace WindowsFormsApplication1 { public partial class Form1 : Form { Customer selectedCustomer; List<Customer

我正在使用从我的客户对象到组合框的数据绑定。我试图实现的行为是,标签文本将反映所选的名称

using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
  public partial class Form1 : Form
  {
    Customer selectedCustomer;
    List<Customer> list = new List<Customer>();

    public Form1()
    {
        InitializeComponent();
        selectedCustomer = new Customer() { Id = 2, FirstName = "Jane" };
    }

    private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
    {
        label1.Text = selectedCustomer.FirstName;
    }

    private void Form1_Load(object sender, EventArgs e)
    {

        list.Add(new Customer() { Id = 1, FirstName = "John" });
        list.Add(new Customer() { Id = 2, FirstName = "Jane" });

        comboBox1.DisplayMember = "FirstName";
        comboBox1.ValueMember = "Id";
        comboBox1.DataSource = list;
        comboBox1.DataBindings.Add("Text", selectedCustomer, "FirstName");
    }
  }

  public class Customer
  {
      public int Id { get; set; }
      public string FirstName { get; set; }
  }
}
使用系统;
使用System.Collections.Generic;
使用System.Windows.Forms;
命名空间Windows窗体应用程序1
{
公共部分类Form1:Form
{
客户选择客户;
列表=新列表();
公共表格1()
{
初始化组件();
selectedCustomer=newcustomer(){Id=2,FirstName=“Jane”};
}
私有无效组合框1\u SelectedIndexChanged(对象发送方,事件参数e)
{
label1.Text=selectedCustomer.FirstName;
}
私有void Form1\u加载(对象发送方、事件参数e)
{
添加(新客户(){Id=1,FirstName=“John”});
添加(新客户(){Id=2,FirstName=“Jane”});
comboBox1.DisplayMember=“FirstName”;
comboBox1.ValueMember=“Id”;
comboBox1.DataSource=列表;
comboBox1.DataBindings.Add(“Text”,selectedCustomer,“FirstName”);
}
}
公共类客户
{
公共int Id{get;set;}
公共字符串名{get;set;}
}
}

您应该将所选项目分配给
selectedCustomer
字段:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    selectedCustomer = (Customer)comboBox1.SelectedItem;
    label1.Text = selectedCustomer.FirstName;
}
如果希望自动更改标签文本,则应为标签添加数据绑定(当前正在将其添加到combobox):

但文本也不会更新。为什么?由于标签绑定到客户的特定实例(绑定添加时为一个实例)-标签将反映其绑定到的客户的变化:

selectedCustomer.FirstName = "Serge";
但同样,若你们改变客户的名字,什么也不会发生。为什么?因为客户应该实现
INotifyPropertyChanged
接口,并引发事件以通知标签名称更改:

public class Customer : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _firstName;

    public int Id { get; set; }

    public string FirstName 
    { 
        get { return _firstName; } 
        set 
        { 
            _firstName = value; // well, it's better to check if value changed
            if (PropertyChanged !=null) 
                PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
        }
    }        
}

现在,若您要更改所选客户的名称,新值将出现在标签中。这就是数据绑定在winforms中的工作方式。

但数据绑定不应该自动更新源(在本例中为selectedCustomer对象)?@Rod的问题是,当选择更改时,您没有更新
selectedCustomer
字段您可以为标签
label1.DataBindings.add(“Text”,selectedCustomer,“FirstName”)添加绑定;
public class Customer : INotifyPropertyChanged
{
    public event PropertyChangedEventHandler PropertyChanged;
    private string _firstName;

    public int Id { get; set; }

    public string FirstName 
    { 
        get { return _firstName; } 
        set 
        { 
            _firstName = value; // well, it's better to check if value changed
            if (PropertyChanged !=null) 
                PropertyChanged(this, new PropertyChangedEventArgs("FirstName"));
        }
    }        
}