繁体   English   中英

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

[英]How to bind a List to a 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 关联

当您指的是组合框时,我假设您不想使用 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") };

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

comboBox1.DataSource = bindingSource1.DataSource;

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

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

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


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

对于后台程序,有两种方法可以使用 ComboBox/ListBox

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

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

繁荣。

尝试这样的事情:

yourControl.DataSource = countryInstance.Cities;

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

yourControl.DataBind();
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";

这是我现在使用的代码。

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM