在Silverlight中在XAML文件中很好定义ControlTemplate,但是在cs文件中就不是很容易,有一种方法是通过XamlReader可以来load出ControlTemplate,那么有没有使用new来写ControlTemplate的,就是ControlTemplate里面的东西都是通过实例化类来实现的

解决方案 »

  1.   

    比较麻烦的。
    参考这个例子:
            public Window1()
            {
                InitializeComponent();            ControlTemplate template = new ControlTemplate();
                template.VisualTree = new FrameworkElementFactory(typeof(TextBox));            
                this.Template = template;
            }
      

  2.   

    三楼的是wpf中的吧?Silverlight中可以做到吗?Silverlight的ControlTemplate  是没有VisualTree 属性的,还有其它的吗,谢谢
      

  3.   

    这样的话就没有办法了。即使在WPF里,FrameworkElementFactory也是deprecated的。
    推荐动态生成XAML
      

  4.   

    ControlTemplate  做了一个string 拼接的的,但是人家说不想使用string,就像new出来创建,唉,这世道 呵呵