【发布时间】:2010-06-07 08:43:16
【问题描述】:
我正在使用 CodePlex 的 DataGrid。
【总结:使用模板选择器时如何绑定动态数据(动态名称指定的对象的属性)?替代解决方案也将被接受。我一直在考虑基于 BoundColumn 制作我自己的,但有人在使用这种方法时遇到了问题。]
我的对象 (ICustomTypeDescriptor) 具有属性 'name' 'description' 'c' 'd' 等等(名称是动态的,值也是动态的)并且它们背后有信息。
我有不同类型的模板。如果我必须列出单个元素的属性,我可以绑定到 xaml 中的值并使用 datatemplateselector(在 datagridtemplatecolumn 中)来选择正确的模板。
但是,当我列出几个元素时,如何使用选择器和模板?如何将正确的属性绑定到模板(双向)?
当忽略对不同模板的需求时,将 DataGridTextColumn 与绑定一起使用非常简单(并且有效,请参见下面的示例),有没有办法将类似的方法与 DataGridTemplateColumn 一起使用?
foreach (String propertyName in listOfPropertyNames)
{
DataGridTextColumn textColumn = new DataGridTextColumn()
{
Header = propertyName,
Binding = new Binding()
{
Path = new PropertyPath(propertyName)
}
};
DataGrid.Columns.Add(textColumn);
}
【问题讨论】:
标签: wpf data-binding datagrid