【发布时间】:2010-11-05 17:10:34
【问题描述】:
我有一个属性网格控件,它有许多使用CellEditorTemplateSelector 自动应用的单元格编辑器。每个属性网格行都绑定到一个简单的 PropertyItemViewModel。
现在,我正在尝试重用所有这些单元格编辑器并将其呈现在 DataGrid 中,以便能够并排比较多个对象值。所以我添加了一个PropertiesRow 对象,其中包含PropertyItemViewModel 列表(与上面的属性网格相同)。
为了呈现每个单元格,我有一个简单的数据模板,它使用与属性网格相同的模板选择器。
<DataTemplate x:Key="CellDataTemplate">
<ContentControl
Content="{Binding Mode=OneWay}"
ContentTemplateSelector="{StaticResource CellEditorTemplateSelector}" />
</DataTemplate>
但是,要使其正常工作,模板需要PropertyItemViewModel(不是PropertiesRow),因此我必须以某种方式通过从PropertiesRow.PropertyItems[columnIndex] 获取正确的绑定来提供它。所以当我通过代码添加列时,我尝试了这样的事情:
void AddColumns()
{
foreach (Property shownProperty in _ShownProperties)
{
_DataGrid.Columns.Add(new DataGridTemplateColumn()
{
Header = shownProperty.Name;
Binding = new Binding("PropertyItems[" + index + "]");
CellTemplate = (DataTemplate) FindResource("CellDataTemplate");
});
}
}
但是,DataGridTemplateColumn 没有 Binding 属性!所以我尝试为每一列生成一个中间 DataTemplate,但这开始变得非常复杂,我觉得必须有更简单的方法来做到这一点。
有什么建议吗?
【问题讨论】:
标签: wpf binding wpfdatagrid