【问题标题】:C# databinding on combobox组合框上的 C# 数据绑定
【发布时间】:2010-11-22 13:31:45
【问题描述】:

鉴于此代码,我无法理解以下 2 个问题。我将一个组合框映射到一个自定义对象,并且我希望每次组合框上的选定值发生变化时,自定义对象也会发生变化。

public partial class MainForm : Form
{
    private Person _person;
    public MainForm()
    {
        InitializeComponent();
        _person = new Person();

        //Populating the combox, we have this.comboBoxCities.DataSource = this.cityBindingSource;
        cityBindingSource.Add(new City("London"));
        cityBindingSource.Add(new City("Paris"));
        _person.BirthCity = new City("Roma");
        cityBindingSource.Add(_person.BirthCity);
        cityBindingSource.Add(new City("Madrid"));

        //Doing the binding
        comboBoxCities.DataBindings.Add("SelectedItem", _person, "BirthCity");
    }

    private void buttonDisplay_Click(object sender, EventArgs e)
    {
        MessageBox.Show("BirthCity=" + _person.BirthCity.Name);
    }

    private int i = 0;
    private void buttonAddCity_Click(object sender, EventArgs e)
    {
        City city = new City("City n°" + i++);
        cityBindingSource.Add(city);
        comboBoxCities.SelectedItem = city;
    }

}

public class Person
{
    private City _birthCity;
    public City BirthCity
    {
        get { return _birthCity; }
        set
        {
            Console.WriteLine("Setting birthcity : " + value.Name);
            _birthCity = value;
        }
    }
}

public class City
{
    public string Name { get; set; }
    public City(string name) { Name = name; }
    public override string ToString() { return Name; }
}

1 - 为什么当我在组合框上连续手动选择两次(或更多)不同的值时,我只收到了一次对 BirthCity.Set 的调用,而没有最后一个选择的值(并且该调用似乎仅在组合框丢失时触发焦点)?

2 - 为什么当我单击 buttonAddCity 然后 buttonDisplay 时,显示的城市不是选择的城市(不是组合框中显示的城市)

【问题讨论】:

  • 你应该重写 GetHashCode() 和 Equals() 方法。

标签: c# data-binding binding combobox


【解决方案1】:
为什么当我在组合框上连续手动选择两次(或更多)不同的值时,我只收到了一次对 BirthCity.Set 的调用,而最后一次选择的值(并且调用似乎仅在组合框失去焦点时触发) ?

这就是数据绑定的工作原理,当验证发生时,数据从控件移动到属性,当控件失去焦点时进行验证。

为什么当我点击 buttonAddCity 然后 buttonDisplay 时,显示的城市不是选择的城市(不是组合框中显示的城市)

我不知道。我创建了一个简单的表单(使用 .Net 3.5 SP1 的 Visual C# Express 2008)并几乎逐字粘贴了您的代码,它按预期工作:它在组合框中显示了新城市。

如果添加comboBoxCities.Focus();到 buttonAddCity_Click () 的末尾,您将确保新城市更早地被推送到 _person.BirthCity 中,而不是在 ValidateChildren () 中。

【讨论】:

  • 谢谢,这正是我不知道的。你有解释数据绑定如何工作的链接吗?其他问题:如果我按照建议添加 comboboxCities.focus() 效果很好,但它不适用于调用 validatechildren,为什么?
  • 你有解释数据绑定如何工作的链接吗?
    遗憾的是没有。我现在正在使用 Visual Studio Express 自学 C#,并且文档相当稀少。我正在阅读 Noyes 的“Data Binding with Windows Forms 2.0”,但这主要是关于使数据绑定与数据库一起工作。
  • 2:如果在添加 SetFocus() 时它“运行良好”,那么您在原始问题中的意思可能是在添加新城市时不会将其推送到该属性,而不是没有出现添加时在组合框中。同样,问题在于,如果您不调用 SetFocus (),则组合框无法失去焦点,从而导致验证和绑定。
  • 3:您应该能够在包含控件(Form、UserControl 等)上调用 ValidateChildren (),该控件调用所有包含控件的验证和绑定。 FormClose 事件是一个显而易见的调用位置,但还有其他的。
  • 到 2 :确实问题是新城市没有被推到 ----- 到 3 :好的,我会尝试找到一个调用 validatechildren() 的好地方,因为调用 SetFocus 并不是真的很好,谢谢
猜你喜欢
  • 2017-11-09
  • 2014-02-03
  • 2011-08-25
  • 1970-01-01
  • 2013-12-24
  • 2016-05-02
  • 2012-01-24
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多