[英]C# Combobox binding to a Dictionary got 1 item when source empty
我在Windows窗体中有以下代码
Dictionary<String, String> dict = new Dictionary<string, string>();
ComboBox cbo = new ComboBox();
cbo.DisplayMember = "Key";
cbo.ValueMember = "Value"; // Not used
this.Controls.Add(cbo);
BindingSource bind = new BindingSource(dict, null);
cbo.DataSource = bind;
int count = cbo.Items.Count;
为什么以我为例,计数最后是1?
如果我将数据放入字典中,一切都会好的。
编辑:解决方案在答案的评论中(直到我们有更好的选择)
您可以尝试更改行:
BindingSource bind = new BindingSource(dict, null);
cbo.DataSource = bind;
至:
BindingSource bind = new BindingSource();
bind.DataSource = dict;
cbo.DataSource = bind;
我尝试了这个, cbo.Items.Count
中的结果为0。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.