【发布时间】:2012-07-20 21:52:25
【问题描述】:
我正在尝试将我的 datagridview 的 itemssource 属性绑定到一个对象列表,这些对象的属性名称直到运行时才知道。
代码当前编译,但列中没有显示任何内容(数据网格为我的列表中的每个项目显示一行,但每列中没有任何内容)
设置列绑定
foreach (KeyValuePair<string, string> pair in _columns)
{
Microsoft.Windows.Controls.DataGridTextColumn textCol = new Microsoft.Windows.Controls.DataGridTextColumn();
textCol.Header = pair.Key;
textCol.Binding = new Binding(pair.Value);
ItemListDataGrid.Columns.Add(textCol);
}
硬编码列表示例:
List<List<KeyValuePair<string,string>>> itemSet = new List<List<KeyValuePair<string,string>>>();
List<KeyValuePair<string,string>> item1 = new List<KeyValuePair<string,string>>();
item1.Add(new KeyValuePair<string,string>("ACTION","ACTION"));
itemSet.Add(item1);
ItemListDataGrid.ItemsSource = itemSet;
任何想法如何让它工作?
【问题讨论】:
-
你没有给我们任何继续。 “我无法让它工作”是什么意思?它抛出异常?向我们展示例外情况。向我们展示代码。
-
该列在 datagridview 中显示为空白,如上所述。编辑:添加代码更清晰
-
为什么不是
List<Dictionary<K,V>>? -
我想将 itemssource 绑定到一组可枚举的对象,直到运行时我才知道键。每个“行”有多个“列”。
标签: c# wpf data-binding wpfdatagrid