[英]Silverlight with using of DependencyProperty and ControlTemplate
[英]Creating a ControlTemplate at Runtime in Silverlight
我正在编写一个Silverlight应用程序,它要求我在运行时动态创建ControlTemplate。 我发现的大多数解决方案都涉及为每个案例创建一个新模板,但我有太多的案例要做。 我如何在C#中创建一个新的ControlTemplate?
您无法单独使用C#在Silverlight中创建ControlTemplate。 与WPF(您可以设置VisualTree属性)不同,您无法设置指定ControlTemplate的“内容”的属性。
您可以将XAML定义为字符串,然后按照此博客文章的说明在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>");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.