簡體   English   中英

當源為空時,綁定到字典的C#組合框獲得1個項目

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM