C# 如何将列表绑定到组合框?
我想将C# 如何将列表绑定到组合框?,c#,winforms,data-binding,combobox,C#,Winforms,Data Binding,Combobox,我想将BindingSource连接到类对象列表,然后将对象值连接到组合框。 有人能建议怎么做吗 public class Country { public string Name { get; set; } public IList<City> Cities { get; set; } public Country() { Cities = new List<City>(); } } 公共类国家 { 公共字符串
BindingSource
连接到类对象列表,然后将对象值连接到组合框。有人能建议怎么做吗
public class Country
{
public string Name { get; set; }
public IList<City> Cities { get; set; }
public Country()
{
Cities = new List<City>();
}
}
公共类国家
{
公共字符串名称{get;set;}
公共IList城市{get;set;}
公共国家()
{
城市=新列表();
}
}
是我的类,我想将其
名称
字段绑定到BindingSource,然后可以将其与组合框关联
yourControl.DataSource = countryInstance.Cities;
如果您使用的是WebForms,则需要添加以下行:
yourControl.DataBind();
当您指的是组合框时,我假设您不想使用双向数据绑定(如果是这样,请查看使用
bindingslist
)
公共类国家
{
公共字符串名称{get;set;}
公共IList城市{get;set;}
公共国家(字符串_名称)
{
城市=新列表();
名称=_名称;
}
}
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";
List countries=新列表{新国家(“英国”),
新国家(“澳大利亚”),
新国家(“法国”);
var bindingSource1=新的BindingSource();
bindingSource1.DataSource=国家/地区;
comboBox1.DataSource=bindingSource1.DataSource;
comboBox1.DisplayMember=“Name”;
comboBox1.ValueMember=“Name”;
要查找在绑定组合框中选择的国家,可以执行以下操作:country country=(country)comboBox1.SelectedItem代码>
如果希望组合框动态更新,则需要确保设置为数据源的数据结构实现IBindingList
;一个这样的结构是BindingList
提示:确保将DisplayMember
绑定到类上的属性,而不是公共字段。如果您的类使用公共字符串名称{get;set;}
它将工作,但如果它使用公共字符串名称代码>它将无法访问该值,而是在组合框中显示每行的对象类型 对于背景,有两种方法可以使用组合框/列表框
1) 将国家/地区对象添加到Items属性,并按Selecteditem检索国家/地区。要使用它,您应该覆盖Country的ToString
2) 使用数据绑定,将数据源设置为IList(列表),并使用DisplayMember、ValueMember和SelectedValue
对于2)您首先需要一份国家列表
// not tested, schematic:
List<Country> countries = ...;
...; // fill
comboBox1.DataSource = countries;
comboBox1.DisplayMember="Name";
comboBox1.ValueMember="Cities";
公共类国家
{
公共字符串名称{get;set;}
公共IList城市{get;set;}
公共国家()
{
城市=新列表();
}
}
公营城市
{
公共字符串名称{get;set;}
}
列表国家=新列表
{
新国家
{
Name=“德国”,
城市=
{
新城{Name=“Berlin”},
新城{Name=“汉堡”}
}
},
新国家
{
Name=“英格兰”,
城市=
{
新城{Name=“伦敦”},
新城{Name=“伯明翰”}
}
}
};
bindingSource1.DataSource=国家/地区;
member_CountryComboBox.DataSource=bindingSource1.DataSource;
member\u CountryComboBox.DisplayMember=“Name”;
成员国组合
Box.ValueMember=“Name”;
这是我现在使用的代码。public main window(){
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");
}
List personList=新列表();
添加(新的人{name=“rob”,年龄=32});
添加(新的人{name=“annie”,年龄=24});
添加(新的人{name=“paul”,年龄=19});
comboBox1.DataSource=个人列表;
comboBox1.DisplayMember=“name”;
comboBox1.SelectionChanged+=新的SelectionChangedEventHandler(comboBox1\u SelectionChanged);
}
无效组合框1_SelectionChanged(对象发送方,SelectionChangedEventArgs e)
{
person-selectedPerson=comboBox1.SelectedItem为person;
Show(selectedPerson.name,“标题显示在此处”);
}
boom.如果您使用的是ToolStripComboBox,则没有暴露的数据源(.NET 4.0):
List someList=newlist();
添加(“值”);
添加(“值”);
添加(“值”);
ToolStripCombox1.Items.AddRange(someList.ToArray());
以及comboBox1.DataBind();函数我在解决方案中看不到它我正在使用winformsWinforms我想要的是帮助我连接国家对象rest的名称字段中的数据值我会解决的谢谢但这里有一点问题运行应用程序时,名称在组合框中不可见…这可能看起来很明显,但事后看来一切都很明显:)能否解释或添加bindingSource1
的声明?System.Windows.Forms.BindingSource bindingSource1;IscomboBox1.DataSource=bindingSource1.DataSource代码>正确吗?或者应该是comboBox1.DataSource=bindingSource1代码>?这里的单向绑定是什么意思?这是否意味着当国家/地区。从代码中添加(“xxx”)
(列表)时,UI将自动更新?在这种情况下,您需要使用ToolstripComboBox.ComboBox.DataSource
。看起来ToolstripComboBox
是普通ComboBox
的包装器。除了SelectionChanged事件似乎不在.NET 4.0中的控件上外,此功能正常。我将其替换为SelectionChangeCommitted,一切正常。
if (comboBox1.Selecteditem != null)
{
comboBox2.DataSource=comboBox1.SelectedValue;
}
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";
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");
}
List<string> someList = new List<string>();
someList.Add("value");
someList.Add("value");
someList.Add("value");
toolStripComboBox1.Items.AddRange(someList.ToArray());