English 中文(简体)
C# 数据对 com贝箱具有约束力
原标题:C# databinding on combobox
  • 时间:2009-08-25 09:28:49
  •  标签:

鉴于这一法典,我无法理解以下两个问题。 我将一个 com子描绘成一个定制物体,我每次都希望 com子上选定的价值变化,习惯也反对改变。

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 - 为什么我用手工方式在一行(或更长时间)中选择了两 value子的不同价值,我只听说生育间隔。 他必须掌握最后选定的价值(而电话似乎只是在 com子失去重点时才发射)?

2 - 因此,当我点击纽顿AddCity和随后又点击Display时,被淡化的城市不是被选择的(不是在 como箱中展示的)城市。

最佳回答
why when I manually select twice in a row (or more) different value on the combobox, I got only one call to BirthCity.Set witht he last selected value (and the call seems firing only when the combobox lost the focus) ?

由此可见,在进行验证时,数据从控制转移到财产,在控制失去重点时进行验证。

why when I click buttonAddCity and then buttonDisplay, the diplayed city is not the one selected (not the one displayed in the comobox )

我不知道。 我创建了一种简单的表格(Visual C# Express,2008年,使用Net 3.5 SP1),并按预期逐字沿用了你的代码:它显示了新城市的 com。

如果是的话,请加上 com。 焦点(CoodCity_Click(Click))到底,你可以确保新城市被推为人。 早就出生,而不是在ValidateChildren出生。

问题回答

暂无回答




相关问题
热门标签