【发布时间】:2011-08-08 09:17:57
【问题描述】:
我写了一个sn-p,用c#代码创建了一个自己的DataTemplate。我将它添加到 datagrid 列的编辑模板中。
当我调用object templateContent = tc.CellTemplate.LoadContent ( ); 时,应用程序崩溃了,并向我抛出了一个异常,即“FrameworkElementFactory 必须位于此操作的密封模板中。”。
这是我创建数据模板的代码。
public override DataTemplate GenerateCellTemplate ( string propertyName )
{
DataTemplate template = new DataTemplate ( );
var textBlockName = string.Format ( "{0}_TextBlock", propertyName );
FrameworkElementFactory textBoxElement = new FrameworkElementFactory ( typeof ( TextBlock ), textBlockName );
textBoxElement.SetBinding ( TextBlock.TextProperty, new Binding ( propertyName ) );
template.VisualTree = textBoxElement;
Trigger trigger = new Trigger ( );
return template;
}
【问题讨论】:
标签: wpf exception datatemplate wpfdatagrid frameworkelementfactory