如何将列表绑定到组合框?

我想将 BindingSource连接到一个类对象列表,然后将对象值连接到一个 ComboBox。
有人能建议怎么做吗?

public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }


public Country()
{
Cities = new List<City>();
}
}

is my class and I want to bind its name field to a BindingSource which could be then associated with a ComboBox

427905 次浏览

试试这样:

yourControl.DataSource = countryInstance.Cities;

如果你正在使用 WebForms,你需要添加这一行:

yourControl.DataBind();

当您引用组合框时,我假设您不希望使用双向数据绑定(如果是这样,请考虑使用 BindingList)

public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }
public Country(string _name)
{
Cities = new List<City>();
Name = _name;
}
}



List<Country> countries = new List<Country> { new Country("UK"),
new Country("Australia"),
new Country("France") };


var bindingSource1 = new BindingSource();
bindingSource1.DataSource = countries;


comboBox1.DataSource = bindingSource1.DataSource;


comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";

要在绑定的组合框中找到选定的国家,可以执行如下操作: Country country = (Country)comboBox1.SelectedItem;

如果希望 ComboBox 动态更新,则需要确保在 DataSource中设置的数据结构实现了 IBindingList; 其中一个结构是 BindingList<T>


提示: 确保将 DisplayMember绑定到类的 Property 而不是公共字段。如果你的类使用 public string Name { get; set; }它将工作,但如果它使用 public string Name;它将不能访问的值,而是将显示对象类型的每一行在组合框。

对于一个背景,有2种方法来使用组合框/列表框

1)向 Items 属性添加 Country Objects,并检索 Country as Selecteditem。要使用此选项,应重写 ToString of Country。

2)使用 DataBinding,将 DataSource 设置为 IList (List < >) ,并使用 DisplayMember、 ValueMember 和 SelectedValue

2)你首先需要一份国家名单

// not tested, schematic:
List<Country> countries = ...;
...; // fill


comboBox1.DataSource = countries;
comboBox1.DisplayMember="Name";
comboBox1.ValueMember="Cities";

然后在 SelectionChanged 中,

if (comboBox1.Selecteditem != null)
{
comboBox2.DataSource=comboBox1.SelectedValue;


}
public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }


public Country()
{
Cities = new List<City>();
}
}


public class City
{
public string Name { get; set; }
}


List<Country> Countries = new List<Country>
{
new Country
{
Name = "Germany",
Cities =
{
new City {Name = "Berlin"},
new City {Name = "Hamburg"}
}
},
new Country
{
Name = "England",
Cities =
{
new City {Name = "London"},
new City {Name = "Birmingham"}
}
}
};
bindingSource1.DataSource = Countries;
member_CountryComboBox.DataSource = bindingSource1.DataSource;
member_CountryComboBox.DisplayMember = "Name";
member_CountryComboBox.ValueMember = "Name";

这是我现在使用的代码。

public MainWindow(){
List<person> personList = new List<person>();


personList.Add(new person { name = "rob", age = 32 } );
personList.Add(new person { name = "annie", age = 24 } );
personList.Add(new person { name = "paul", age = 19 } );


comboBox1.DataSource = personList;
comboBox1.DisplayMember = "name";


comboBox1.SelectionChanged += new SelectionChangedEventHandler(comboBox1_SelectionChanged);
}




void comboBox1_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
person selectedPerson = comboBox1.SelectedItem as person;
messageBox.Show(selectedPerson.name, "caption goes here");
}

砰。

If you are using a ToolStripComboBox there is no DataSource exposed (.NET 4.0):

List<string> someList = new List<string>();
someList.Add("value");
someList.Add("value");
someList.Add("value");


toolStripComboBox1.Items.AddRange(someList.ToArray());

As a small addition to this, I tried to incorporate something similar to this code, and was frustrated that adding/removing from the list was not reflected in the ComboBox. This is because the Add/Remove does not trigger the OnPropertyChange.

如果希望添加/删除并将它们反映在 ComboBox 中,则需要将 List < > 更改为 Observer ableCollection

List<Country> Countries

应该换成

    private ObservableCollection<Country> countries;
public ObservableCollection<Country> Countries
{
get { return countries; }
set
{
countries= value;
OnPropertyChanged("Countries");
}
}

OnPropertyChanged 和 Observer ableCollection 从何而来

using System.Runtime.CompilerServices;
using System.Collections.ObjectModel;
public event PropertyChangedEventHandler PropertyChanged;
protected void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChangedEventHandler handler = this.PropertyChanged;
if (handler != null)
{
var e = new PropertyChangedEventArgs(propertyName);
handler(this, e);
}
}

所有这些都在前面的解释 给你中得到了更有说服力的表达