刚开始学习数据绑定的东西,
private void Form1_Load(object sender, EventArgs e) { IList<TLayer> tt = new List<TLayer>(); tt.Add(new TLayer("Layer1", "1")); tt.Add(new TLayer("Layer2", "2")); tt.Add(new TLayer("Layer3", "3")); tt.Add(new TLayer("Layer4", "4")); tt.Add(new TLayer("Layer5", "5")); comboBox1.DataSource = tt; comboBox1.DisplayMember = "NameN"; comboBox1.ValueMember = "FL"; } public class TLayer { public string NameN;public string FL;public TLayer(string NameN, string FL) { this.NameN = NameN; this.FL = FL; } }
代码如上,显示是这个样子的
网上的示例 http://www.cnblogs.com/shuang121/archive/2012/11/21/2780819.html
仔细对比发现,唯一的区别是示例中是对公有字段进行了属性封装。而这就是问题关键,修改后如下,即可正常显示
public class TLayer { public string NameN { get; set; } public string FL { get; set; } public TLayer(string NameN, string FL) { this.NameN = NameN; this.FL = FL; } }