如何将列表绑定到组合框? (的WinForms)
我想连接绑定源类对象的列表,然后对象的值组合框任何人都可以建议如何做到这一点如何将列表绑定到组合框? (的WinForms)
public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }
public Country()
{
Cities = new List<City>();
}
}
是我的课,我想结合它的名字场绑定源它是用组合框,然后进行相关的
当你指的是一个组合框,我假设你不希望使用2路数据绑定(如果是这样,看看使用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") };
bindingSource1.DataSource = countries;
comboBox1.DataSource = bindingSource1.DataSource;
comboBox1.DisplayMember = "Name";
comboBox1.ValueMember = "Name";
谢谢,但有点问题,在运行应用程序 – Mobin 2009-03-02 00:30:51
我没有在组合框中看到名称我刚刚创建了一个WinForms应用程序与该代码,它工作正常。 – 2009-03-02 00:32:39
@Mobin如果名称不可见,请确保您将DisplayMember绑定到类的属性上,而不是公共字段。如果你的类使用`public string Name {get;组; }`它会工作,但如果它使用`public string Name';`它将无法访问该值,而是显示组合框中每行的对象类型。 – 2010-06-30 21:13:23
对于一个背景介绍,有2种方式使用ComboBox/ListBox中
1)增加国家的OBJ影响项目属性并将国家检索为Selecteditem。要使用这个,你应该重写国家的ToString。
2)使用数据绑定,设置数据源的IList的(名单<>),并使用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");
}
繁荣。
如果您使用的是ToolStripComboBox控件没有暴露的数据源(.NET 4.0):
List<string> someList = new List<string>();
someList.Add("value");
someList.Add("value");
someList.Add("value");
toolStripComboBox1.Items.AddRange(someList.ToArray());
的WinForms我想是帮我的数据值连接在国家物体静止名现场我会弄清楚 – Mobin 2009-03-02 00:14:58