如何将List绑定到ComboBox?

时间:2009-03-01 23:59:24

标签: c# winforms data-binding combobox

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

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

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

是我的类,我希望将其name字段绑定到BindingSource,然后可以将其与ComboBox相关联

6 个答案:

答案 0 :(得分:143)

正如您所说的组合框,我假设您不想使用双向数据绑定(如果是这样,请查看使用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";

要查找在绑定组合框中选择的国家/地区,您可以执行以下操作:Country country = (Country)comboBox1.SelectedItem;

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


提示:确保将DisplayMember绑定到类上的属性而不是公共字段。如果您的班级使用public string Name { get; set; },那么它会起作用,但如果它使用public string Name;,它将无法访问该值,而是会在组合框中显示每一行的对象类型。

答案 1 :(得分:24)

对于背景资料,有两种方法可以使用ComboBox / ListBox

1)将Country Objects添加到Items属性并检索Country as Selecteditem。要使用它,您应该覆盖Country的ToString。

2)使用DataBinding,将DataSource设置为IList(List&lt;&gt;)并使用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;

}

答案 2 :(得分:20)

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");
}

吊杆。

答案 3 :(得分:0)

尝试这样的事情:

yourControl.DataSource = countryInstance.Cities;

如果您使用的是WebForms,则需要添加以下行:

yourControl.DataBind();

答案 4 :(得分:0)

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_CountryCombo

Box.ValueMember = "Name";

这是我现在使用的代码。

答案 5 :(得分:-1)

如果您使用的是ToolStripComboBox,则不会公开DataSource(.NET 4.0):

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

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