Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/285.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在Silverlight中在运行时创建ControlTemplate_C#_Silverlight_Runtime_Controltemplate - Fatal编程技术网

C# 在Silverlight中在运行时创建ControlTemplate

C# 在Silverlight中在运行时创建ControlTemplate,c#,silverlight,runtime,controltemplate,C#,Silverlight,Runtime,Controltemplate,我正在编写一个Silverlight应用程序,它要求我在运行时动态创建一个ControlTemplate。我发现的大多数解决方案都涉及为每个案例创建一个新模板,但我有太多的案例无法做到这一点。如何在C#中创建新的ControlTemplate?您不能仅在C#中在Silverlight中创建ControlTemplate。与WPF(您可以在其中设置属性)不同,您不能设置任何属性来指定ControlTemplate的“内容” 您可以将XAML定义为字符串,然后在C#中动态加载该字符串,如下所述 代码

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

您不能仅在C#中在Silverlight中创建ControlTemplate。与WPF(您可以在其中设置属性)不同,您不能设置任何属性来指定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>");
var-template=(ControlTemplate)XamlReader.Load(“”)+
“此处的内容”+
"");