带有文本和值的 C# ComboBox
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10062810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C# ComboBox with Text and Value
提问by Chris Watts
Possible Duplicate:
C# Winforms Combobox with Label and Value
可能的重复:
带有标签和值的 C# Winforms 组合框
How would one approach storing a display value and a real value in a ComboBox?
一种方法将如何在 ComboBox 中存储显示值和实际值?
Ie, the ComboBox displays:
即,组合框显示:
- Destroy World
- Fire Slingshot
- Summon Cthulhu
- 毁灭世界
- 火弹弓
- 召唤克苏鲁
but the values as retrieved are:
但检索到的值是:
- dw
- ss
- sc
- 体重
- SS
- sc
I want to be able to retrieve the value of the selected item in a way similar to this:
我希望能够以与此类似的方式检索所选项目的值:
string selectedValue = combobox1.SelectedValue
Updated code in response to answers:
更新代码以响应答案:
Dictionary<string, string> filterItems = new Dictionary<string, string>
{
{"Destroy World", "dw"},
{"Fire Slingshot", "fs"},
{"Summon Cthulu", "sc"},
};
this.options_filterby.DataSource = new BindingSource(filterItems, null);
this.options_filterby.DisplayMember = "Key";
this.options_filterby.ValueMember = "Value";
Now for some reason, although the DisplayMembers are absolutely fine, the ValueMembers return dictionary objects. Even stranger, after a while, eventually the ValueMembers will return strings as expected.
现在出于某种原因,虽然 DisplayMembers 绝对没问题,但 ValueMembers 返回字典对象。更奇怪的是,一段时间后,最终 ValueMembers 将按预期返回字符串。
private void options_filterby_SelectedIndexChanged(object sender, EventArgs e)
{
MessageBox.Show(options_filterby.SelectedValue.ToString());
}
This returns dictionaries for the first few times I change the selected item of the ComboBox, but eventually returns strings as needed.
这将在我更改 ComboBox 的选定项目的前几次返回字典,但最终根据需要返回字符串。
Update: fixed (can't add as solution because question was closed)
更新:已修复(无法添加为解决方案,因为问题已关闭)
In response to the above problem, the fix is to set the DisplayMember and ValueMember properties beforethe DataSource. I presume this is a bug. The code should read:
针对上述问题,修复方法是在 DataSource之前设置 DisplayMember 和 ValueMember 属性。我认为这是一个错误。代码应为:
this.options_filterby.DisplayMember = "Key";
this.options_filterby.ValueMember = "Value";
this.options_filterby.DataSource = new BindingSource(filterItems, null);
回答by Hans Passant
The ComboBox.Items collection stores values of type object. So it can store any kind of type you desire. It generates the displayvalue from the object's ToString() method. To obtain the realvalue, simply cast the object to the type of your class.
ComboBox.Items 集合存储object类型的值。所以它可以存储你想要的任何类型。它从对象的 ToString() 方法生成显示值。要获得真正的值,只需将对象强制转换为您的类的类型。
回答by Patrick
You use the DisplayMemberand ValueMemberto determine what the ComboBox will display, and what will be returned from SelectedValue. When you set the DataSourceproperty, the ComboBox will use the property described by DisplayMemberto render a string to the user.
您使用DisplayMember和ValueMember来确定 ComboBox 将显示什么,以及从SelectedValue. 设置DataSource属性时,ComboBox 将使用 描述的属性DisplayMember向用户呈现字符串。
Something like this
像这样的东西
public class Item {
string Name { get; set; }
string Value { get; set; }
}
ComboBox box = new ComboBox();
box.DisplayMember = "Name";
box.ValueMember = "Value";
box.DataSource = new [] { new Item() { "Test", "test" } };
If you don't set ValueMemberthe actual Item is returned instead, and if you don't set DisplayMember, the items ToString()method will be used to get the string presented to the user.
如果您没有设置ValueMember,则返回实际的 Item,如果您没有设置DisplayMember,则 itemsToString()方法将用于获取呈现给用户的字符串。
I'm not sure if this will work or if it may change what you have, but you could try it at least :)
The thing is, I'm not certain what BindingSource does when it gets a dictionary as its datasource. I suppose it treats it as an IEnumerable<KeyValuePair<>>though, so your code should work, but well, it doesn't, so perhaps this will..
我不确定这是否可行,或者它是否会改变您拥有的内容,但您至少可以尝试一下:) 问题是,当 BindingSource 将字典作为其数据源时,我不确定它会做什么。我想它把它当作一个IEnumerable<KeyValuePair<>>虽然,所以你的代码应该可以工作,但好吧,它没有,所以也许这会......
BindingSource source = new BindingSource();
source.DataSource = typeof(KeyValuePair<string, string>);
foreach (KeyValuePair<string, string> pair in filterItems) {
source.Add(pair);
}
options_filterby = source;

