【问题标题】:FrameworkElementFactory must be in a sealed template for this operationFrameworkElementFactory 必须位于此操作的密封模板中
【发布时间】: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


    【解决方案1】:

    我在反射器中反射框架模板代码。我发现 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;
    }
    

    【讨论】:

    • 我一直在使用 Telerik GridView 动态创建 DataTemplates,并且必须调用 Seal() 才能使其工作。你知道为什么吗?我找不到任何应该使用它的示例?
    猜你喜欢
    • 2022-08-03
    • 2016-01-19
    • 1970-01-01
    • 1970-01-01
    • 2012-03-04
    • 1970-01-01
    • 1970-01-01
    • 2020-05-09
    • 2015-05-27
    相关资源
    最近更新 更多