带有文本和值的 C# ComboBox [英] C# ComboBox with Text and Value

查看:20
本文介绍了带有文本和值的 C# ComboBox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可能的重复:
带有标签和值的 C# Winforms 组合框

如何在 ComboBox 中存储显示值和实际值?

How would one approach storing a display value and a real value in a ComboBox?

即,组合框显示:

  • 毁灭世界
  • 火弹弓
  • 召唤克苏鲁

但检索到的值是:

  • dw
  • 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

根据答案更新代码:

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";

现在出于某种原因,虽然 DisplayMembers 绝对没问题,但 ValueMembers 返回字典对象.更奇怪的是,经过一段时间后,最终 ValueMembers 会按预期返回字符串.

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.

private void options_filterby_SelectedIndexChanged(object sender, EventArgs e)
{
    MessageBox.Show(options_filterby.SelectedValue.ToString());
}

这会在前几次我更改 ComboBox 的选定项目时返回字典,但最终根据需要返回字符串.

This returns dictionaries for the first few times I change the selected item of the ComboBox, but eventually returns strings as needed.

针对上述问题,修复方法是在DataSource 之前设置DisplayMember 和ValueMember 属性.我认为这是一个错误.代码应为:

In response to the above problem, the fix is to set the DisplayMember and ValueMember properties before the DataSource. I presume this is a bug. The code should read:

this.options_filterby.DisplayMember = "Key";
this.options_filterby.ValueMember = "Value";
this.options_filterby.DataSource = new BindingSource(filterItems, null);

推荐答案

您使用 DisplayMemberValueMember 来确定 ComboBox 将显示什么,以及将返回什么来自 SelectedValue.当您设置 DataSource 属性时,ComboBox 将使用 DisplayMember 描述的属性向用户呈现字符串.

You use the DisplayMember and ValueMember to determine what the ComboBox will display, and what will be returned from SelectedValue. When you set the DataSource property, the ComboBox will use the property described by DisplayMember to render a string to the user.

类似的东西

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" } };

如果你没有设置ValueMember,则返回实际的Item,如果你没有设置DisplayMember,则返回ItemToString() 方法将用于获取呈现给用户的字符串.

If you don't set ValueMember the 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.

我不确定这是否会奏效,或者它是否会改变你所拥有的,但你至少可以尝试一下:)问题是,当 BindingSource 获取字典作为其数据源时,我不确定它会做什么.我想它把它当作一个 IEnumerable>,所以你的代码应该可以工作,但好吧,它没有,所以也许这会..

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 source = new BindingSource();
source.DataSource = typeof(KeyValuePair<string, string>);
foreach (KeyValuePair<string, string> pair in filterItems) {
    source.Add(pair);
}
options_filterby = source;

这篇关于带有文本和值的 C# ComboBox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆