首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >此操作的FrameworkElementFactory必须在密封的模板中

此操作的FrameworkElementFactory必须在密封的模板中
EN

Stack Overflow用户
提问于 2011-08-08 17:17:58
回答 1查看 5.3K关注 0票数 7

我用c#代码编写了一段代码来创建自己的DataTemplate。并将其添加到datagrid列的编辑模板中。当我调用object templateContent = tc.CellTemplate.LoadContent ( );时,应用程序崩溃了,并抛给我一个异常:"FrameworkElementFactory必须在这个操作的密封模板中。“这是我创建数据模板的代码。

代码语言:javascript
复制
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;
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-08-09 09:46:40

我在reflector中反映了框架模板代码。我发现tc.CellTemplate.LoadContent ()与FrameworkTemplate类中名为"_sealed“的私有字段有关。

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

以下是解决方案:

代码语言:javascript
复制
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;
}
票数 19
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6979974

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档