我一直在尝试在组合框中显示值和键,但到目前为止,只有在字典中我才能使用字典
Data = Dictionary<string, Option<double>>
选项只是允许空值(如果在检索时不可用,则为可选项)。
目前正在使用此功能,只显示值
var list = (from entry in Data select entry.Value).ToList();
DistanceSelectionForm.DistanceList = list.SelectMany(r =>
r).ToArray().ToSeq();
public ISeq<double> radarDistanceList {
set {
DistanceComboBox.DataSource = new BindingSource(value,null);
}
}
这就是我尝试过的,但没有用到
ComboBox.DataSource = new BindingSource(value,null);
ComboBox.DisplayMember = "Key"
ComboBox.ValueMember = "Value";
由于
答案 0 :(得分:0)
尝试使用对象列表而不是字典。每个对象都有一个键和一个值,您将用于DisplayMember和ValueMember。 这是一个只有组合框的表单的完整示例:
public class myClass
{
private string key;
private string val;
public string Key
{
get
{
return key;
}
set
{
key = value;
}
}
public string Value
{
get
{
return val;
}
set
{
val = value;
}
}
public myClass(string newKey, string newVal)
{
key = newKey;
val = newVal;
}
}
public partial class Form1 : Form
{
List<myClass> list = new List<myClass>();
public Form1()
{
InitializeComponent();
list.Add(new myClass("a","aa"));
list.Add(new myClass("b", "bb"));
list.Add(new myClass("v", "vv"));
comboBox1.DataSource = list;
comboBox1.DisplayMember = "Key";
comboBox1.ValueMember = "Value";
}
}