C# WPF DataGrid - 循环中的 ExpandoObject

C# WPF DataGrid - ExpandoObject in loop

我对该代码有一些疑问。我做错了什么?

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

list.Add(new ExpandoObject());

foreach (var c in columnBind)
{
    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
}

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

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