yaf*_*eya 6 wpf exception datatemplate wpfdatagrid frameworkelementfactory
我编写了一个片段来通过c#代码创建自己的DataTemplate.然后我将它添加到datagrid列的编辑模板中.当我调用时object templateContent = tc.CellTemplate.LoadContent ( );,应用程序崩溃,并抛出一个异常,"FrameworkElementFactory必须在此操作的密封模板中.".这是我创建datatemplate的代码.
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;
}
Run Code Online (Sandbox Code Playgroud)
yaf*_*eya 15
我在反射器中反映了框架模板代码.我发现tc.CellTemplate.LoadContent()与FrameworkTemplate类中名为"_sealed"的私有字段有关.
然后我找到了设置值的字段,我调用这个方法,问题解决了.
这是解决方案:
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 ( );
// This solves it!
template.Seal();
return template;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4821 次 |
| 最近记录: |