在Silverlight中的运行时创建ControlTemplate

时间:2011-07-26 16:49:03

标签: c# silverlight runtime controltemplate

我正在编写一个Silverlight应用程序,它要求我在运行时动态创建ControlTemplate。我发现的大多数解决方案都涉及为每个案例创建一个新模板,但我有太多的案例要做。我如何在C#中创建一个新的ControlTemplate?

1 个答案:

答案 0 :(得分:3)

您无法单独使用C#在Silverlight中创建ControlTemplate。与WPF(您可以设置VisualTree属性)不同,您无法设置指定ControlTemplate“内容”的属性。

您可以将XAML定义为字符串,然后按照此blog post的说明在C#中动态加载。

代码归结为:

var template = (ControlTemplate)XamlReader.Load("<ControlTemplate " +                 
       " xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\"" +
       " xmlns:x=\"http://schemas.microsoft.com/winfx/2006/xaml\">" +
       " content here " +
       "</ControlTemplate>");
相关问题