在我工作的一个应用程序中,我发现了这段代码 -
public class MatrixCellTemplate : ColumnDataTemplate<MatrixCellContainer>
{
}
public class ColumnDataTemplate<T> : DataTemplate where T : FrameworkElement
{
public ColumnDataTemplate()
{
FrameworkElementFactory factory = new FrameworkElementFactory(typeof(T));
VisualTree = factory;
}
}
Run Code Online (Sandbox Code Playgroud)
这MatrixCellTemplate用于设置CellTemplate自定义DataGridTemplateColumn(稍后添加到DataGrid.Columns集合),如下所示 -
<DataGridTemplateColumn.CellTemplate>
<Matrix:MatrixCellTemplate />
</DataGridTemplateColumn.CellTemplate>
Run Code Online (Sandbox Code Playgroud)
我不确定使用这个有FrameworkElementFactory什么好处,如果我直接MatrixCellContainer用作细胞模板,我可以面对什么问题-
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<Matrix:MatrixCellContainer>
</Matrix:MatrixCellContainer>
</DataTemplate>
<!--<Matrix:MatrixCellTemplate />-->
</DataGridTemplateColumn.CellTemplate>
Run Code Online (Sandbox Code Playgroud) wpf datatemplate wpfdatagrid celltemplate frameworkelementfactory