如何绑定列表来组合框? (的WinForms) [英] how to bind a list to a combobox? (Winforms)

查看:215
本文介绍了如何绑定列表来组合框? (的WinForms)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想连接绑定源类对象的列表,然后对象的价值组合框任何人都可以提出如何做到这一点。

I want to connect a binding source to a list of class objects and then objects value to a combo box 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>();
        }
    }

是我的班,我想它的名字字段绑定到可以用一个组合框,然后进行相关的绑定源

is my class and i want to bind its name field to a binding source which could be then associated with a combobox

推荐答案

你指的是一个组合框,我假设你不希望使用2路数据绑定(如果是这样,看看使用的BindingList

As you are referring to a combobox, I'm assuming you don't want to use 2-way databinding (if so, look at using a 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";

这篇关于如何绑定列表来组合框? (的WinForms)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆