1

我有课

public class CLASS_K
{
    public static List<AA> simpleList = new List<AA>()
    {
        new AA(){NNN = "o1", NNN2 = "o1a"},
        new AA(){NNN = "o2", NNN2 = "o2a"}
    };

    public class AA
    {
        public string NNN { get; set; }
        public string NNN2 { get; set; }
    }
}

我为标签设置绑定:

label1.DataBindings.Add("text", CLASS_K.simpleList[1], "NNN");

如何将 dataSource 从标签转换为 AA 类?

我的错误想法:

BindingSource binding = (BindingSource)label1.DataBindings[0].DataSource;
CLASS_K.AA b = (CLASS_K.AA) binding.DataSource;
4

1 回答 1

0

DataSource包含类型的对象CLASS_K.AA,所以试试这个:

if (label1.DataBindings.Count != 0)
{
    var b = label1.DataBindings[0].DataSource as CLASS_K.AA;
    if (b != null)
    {
        // do something...
    }
}  
于 2013-09-14T23:49:21.293 回答