C#WPF DataGrid - 循环中的ExpandoObject

时间:2016-06-04 16:00:14

标签: c# wpf datagrid expandoobject

我的代码存在问题。我做错了什么?

List<dynamic> list = new List<dynamic>();
var collumnBind = columnBindName(); // {id, n0, n1, etc...} Name of Columns Bind

list.Add(new ExpandoObject());

foreach (var c in collumnBind)
{
    list[0].c = "something";    //not working in Datagrid
    Console.Write(list[0].c);   //in console i have "something"
    list[0].id = "hello";       //working in Datagrid
}

1 个答案:

答案 0 :(得分:2)

假设您要设置在自定义列表中定义的属性名称,columnBindName是某种string可枚举的,您可以使用ExpandoObject实现IDictionary<string, object>接口的功能您可以通过字符串名称获取/设置属性值

foreach (var c in collumnBind)
{
    (list[0] as IDictionary<string, object>)[c] = "property value";
}