0
我在設置ComboBox的ValueMember屬性時遇到了一些問題。C#combobox ValueMember
行comboBox1.ValueMember = "Code";
打破我的代碼。
這裏是我的代碼:
Form1.cs中:
private void Form1_Load(object sender, EventArgs e) {
...
...
MAPList MAP = new MAPList();
comboBox1.DataSource = MAP.All;
comboBox1.ValueMember = "Code";
...
...
}
MAPList.cs:
public class MAPList {
public readonly List<MAP> All;
public MAPList() {
All = new List<MAP>();
var MapData = // Getting map data
foreach(MAP m in MapData) {
All.Add(new Map(m.Name, m.Code));
}
}
}
MAP.cs:
public class MAP {
public readonly string Name;
public readonly string Code;
public RadioCode(string name, string code) {
Name = name;
Code = code;
}
public override string ToString() {
return String.Format("{0}: {1}", Name, Code);
}
}
哇...這是奇怪! Code作爲一個公共只讀成員,爲什麼它不工作? – Moon 2010-09-08 04:25:18
@Ruby ComboBox使用反射從底層對象獲取值。在這個過程中,它正在尋找屬性而不是字段 – 2010-09-08 04:30:47