码迷,mamicode.com
首页 > 其他好文 > 详细

WPF在代码中创建DataTemplate时候的异常

时间:2014-04-28 15:39:11      阅读:603      评论:0      收藏:0      [点我收藏+]

标签:com   http   blog   style   img   code   size   width   color   log   404   

今天写段程序用到了在代码中手动创建DataTemplate,

    var factory = new FrameworkElementFactory(typeof(OperationColumn));
    returnnewDataTemplate() { VisualTree = factory };

运行的时候出现如下异常: FrameworkElementFactory 必须位于此操作的密封模板中。

在 System.Windows.FrameworkElementFactory.InstantiateUnoptimizedTree()
在 System.Windows.FrameworkTemplate.LoadContent()

  mamicode.com,码迷

平时我也是这么些的,一直都是好好的,不知道这次是不是用了一个第三方控件的缘故。网上搜了一下,后在StackOverFlow上找到了解决方案: FrameworkElementFactory must be in a sealed template for this operation

具体的做法是:创建了DataTemplate后,调用Seal函数锁定模板。

    var factory = newFrameworkElementFactory(typeof(OperationColumn));
    var dataTemplate = newDataTemplate() { VisualTree = factory };
    
dataTemplate.Seal();
    return dataTemplate;

WPF在代码中创建DataTemplate时候的异常,码迷,mamicode.com

WPF在代码中创建DataTemplate时候的异常

标签:com   http   blog   style   img   code   size   width   color   log   404   

原文地址:http://www.cnblogs.com/TianFang/p/3695326.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!