이 코드에서 주어진 다음 2 가지 문제점을 이해할 수 없습니다. 나는 커스텀 오브젝트에 콤보 박스를 맵핑했고, 콤보 박스에서 선택된 값이 변경 될 때마다 커스텀 오브젝트가 변경되기를 바란다.콤보 상자에서 C# 데이터 바인딩
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을 클릭하면 미리 보여줍니다 도시는 선택된 하나 (안 comobox에 표시되는 것)
GetHashCode() 및 Equals() 메서드를 재정의해야합니다. – Rock