我正在使用.NET 2.0,并尝试将组合框的数据源绑定到排序字典。
所以我得到的错误是“在数据源上找不到DataMember属性'Key‘”。
SortedDictionary<string, int> userCache = UserCache.getSortedUserValueCache();
userListComboBox.DataSource = new BindingSource(userCache, "Key"); //This line is causing the error
userListComboBox.DisplayMember = "Key";
userListComboBox.ValueMember = "Value";
发布于 2011-06-20 22:35:25
SortedDictionary<string, int> userCache = new SortedDictionary<string, int>
{
{"a", 1},
{"b", 2},
{"c", 3}
};
comboBox1.DataSource = new BindingSource(userCache, null);
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
但是为什么要将ValueMember
设置为"Value",难道不应该将它绑定到"Key“(以及将DisplayMember
绑定到"Value”)吗?
发布于 2013-04-08 21:37:41
我使用了Sorin Comanescu的解决方案,但在尝试获取所选值时遇到了问题。我的组合框是一个工具条组合框。我使用了" combobox“属性,它公开了一个普通的combobox。
我有一个
Dictionary<Control, string> controls = new Dictionary<Control, string>();
绑定代码(Sorin Comanescu的解决方案-工作起来很棒):
controls.Add(pictureBox1, "Image");
controls.Add(dgvText, "Text");
cbFocusedControl.ComboBox.DataSource = new BindingSource(controls, null);
cbFocusedControl.ComboBox.ValueMember = "Key";
cbFocusedControl.ComboBox.DisplayMember = "Value";
问题是,当我试图获取选定的值时,我没有意识到如何检索它。经过几次尝试,我得到了这样的结果:
var control = ((KeyValuePair<Control, string>) cbFocusedControl.ComboBox.SelectedItem).Key
希望对其他人有所帮助!
发布于 2014-10-08 22:28:14
var colors = new Dictionary < string, string > ();
colors["10"] = "Red";
绑定到组合框
comboBox1.DataSource = new BindingSource(colors, null);
comboBox1.DisplayMember = "Value";
comboBox1.ValueMember = "Key";
完整源代码...Dictionary as a Combobox Datasource
Jeryy
https://stackoverflow.com/questions/6412739
复制相似问题