【发布时间】:2011-07-26 16:49:03
【问题描述】:
我正在编写一个 Silverlight 应用程序,它要求我在运行时动态创建一个 ControlTemplate。我发现的大多数解决方案都涉及为每个案例创建一个新模板,但我有太多案例无法做到这一点。如何在 C# 中创建新的 ControlTemplate?
【问题讨论】:
标签: c# silverlight runtime controltemplate
我正在编写一个 Silverlight 应用程序,它要求我在运行时动态创建一个 ControlTemplate。我发现的大多数解决方案都涉及为每个案例创建一个新模板,但我有太多案例无法做到这一点。如何在 C# 中创建新的 ControlTemplate?
【问题讨论】:
标签: c# silverlight runtime controltemplate
您不能单独使用 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>");
【讨论】: