gpt4 book ai didi

wpf - FrameworkElementFactory 必须位于此操作的密封模板中

转载 作者:行者123 更新时间:2023-12-04 13:24:36 24 4
gpt4 key购买 nike

我编写了一个片段来通过 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;
}

最佳答案

我在反射器中反射(reflect)框架模板代码。而且我发现 tc.CellTemplate.LoadContent () 与 FrameworkTemplate 类中名为“_sealed”的私有(private)字段有关。

然后我找到了设置值的字段,我调用了这个方法,问题就解决了。

这是解决方案:

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;
}

关于wpf - FrameworkElementFactory 必须位于此操作的密封模板中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6979974/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com