public class ObjectsConvertToUIFrmws {
public IList BuildUIList(IList objs) {
return objs.Cast
測試實體
public class TestDataItem {
[Display(Name = "名字:")] public string Name { get; set; }
[Display(Name = "年齡:")] public int Age { get; set; }
[Display(Name = "城市:")] public string City { get; set; }
public Color BackGround { get; set; } // 無需 Display,作為背景色
}
使用
var list = new List {
new() { Name = "abc", Age = 12, City = "大連", BackGround = Colors.Yellow },
new() { Name = "def", Age = 20, City = "上海", BackGround = Colors.Pink },
new() { Name = "gao", Age = 60, City = "珠海", BackGround = Colors.SkyBlue }
};
var converter = new ObjectsConvertToUIFrmws();
lbList.ItemsSource = converter.BuildUIList(list);