English 中文(简体)
如何将列表绑定到ComboBox?
原标题:
  • 时间:2009-03-01 23:59:23
  •  标签:

I want to connect a BindingSource to a list of class objects and then objects value to a ComboBox.
Can anyone suggest how to do it?

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

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

这是我的类,我想将它的name字段绑定到一个BindingSource上,然后与ComboBox相关联。

最佳回答

由于您提到了下拉框,我假设您不想使用双向数据绑定(如果想使用,请考虑使用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;
    }
}

Sorry, you have not provided any text to be translated into Chinese. Please provide the text you want to translate.

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 绑定到类上的属性而不是公共字段。如果您的类使用public string Name {get; set;},它将起作用,但如果它使用public string Name;它将无法访问该值,并且将显示组合框中每行的对象类型。

问题回答

作为背景说明,ComboBox/ListBox有两种使用方式。

1) 将国家对象添加到项属性中并检索选择项目的国家。为使用此功能,您应该重写Country的ToString方法。

2)使用数据绑定,将数据源设置为 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();

如果您正在使用ToolStripComboBox,则没有公开的数据源(.NET 4.0):

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

toolStripComboBox1.Items.AddRange(someList.ToArray());
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";

这是我现在正在使用的代码。

作为对此的小补充,我尝试将类似于此代码的东西整合进去,但很沮丧的是,在列表中添加/删除的更改没有反映在组合框中。这是因为添加/删除没有触发OnPropertyChange。

如果您想添加/删除并且希望它们在ComboBox中反映出来,您需要将List<>更改为ObservableCollection。

List<Country> Countries

应该被替换为

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

"PropertyChanged 和 ObservableCollection 来自哪里"

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

所有这些都在先前的解释中更雄辩地表述了在这里





相关问题
热门标签