【问题标题】:How to bind a List to a ComboBox?如何将列表绑定到组合框?
【发布时间】:2010-10-10 16:40:40
【问题描述】:

我想将 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 关联

【问题讨论】:

  • Winforms 我想要的是帮助我连接国家对象的名称字段中的数据值,我会弄清楚的

标签: c# winforms data-binding combobox


【解决方案1】:

试试这样的:

yourControl.DataSource = countryInstance.Cities;

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

yourControl.DataBind();

【讨论】:

  • 以及comboBox1.DataBind();功能我在使用 winforms 的解决方案中看不到它
【解决方案2】:

当您提到组合框时,我假设您不想使用 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;

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


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

【讨论】:

  • ...这可能看起来很明显,但事后看来一切都很明显:)
  • 能否解释或补充bindingSource1的声明?
  • System.Windows.Forms.BindingSource bindingSource1;
  • comboBox1.DataSource = bindingSource1.DataSource; 正确吗?或者应该是comboBox1.DataSource = bindingSource1;
  • 这里的单向绑定是什么意思?是不是说当countries.Add("xxx")(List) from code时,UI会自动更新?
【解决方案3】:

对于后台程序,有两种方法可以使用组合框/列表框

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;

}

【讨论】:

  • 谢谢,但这里有点问题,运行应用程序时,名称在组合框中不可见
【解决方案4】:
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";

这是我现在使用的代码。

【讨论】:

    【解决方案5】:
    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");
    }
    

    轰隆隆。

    【讨论】:

    • 除了 SelectionChanged 事件似乎不在 .NET 4.0 中的控件上之外,此方法有效。我用 SelectionChangeCommitted 替换了它,一切都很好。
    【解决方案6】:

    如果您使用的是 ToolStripComboBox,则没有暴露数据源(.NET 4.0):

    List<string> someList = new List<string>();
    someList.Add("value");
    someList.Add("value");
    someList.Add("value");
    
    toolStripComboBox1.Items.AddRange(someList.ToArray());
    

    【讨论】:

    • 在这种情况下,您需要使用ToolstripComboBox.ComboBox.DataSource。看起来ToolstripComboBox 是普通ComboBox 的包装器。
    猜你喜欢
    • 2014-03-16
    • 1970-01-01
    • 1970-01-01
    • 2016-04-23
    • 2018-06-20
    • 1970-01-01
    • 2011-09-01
    • 2011-06-28
    相关资源
    最近更新 更多