簡體   English   中英

在Silverlight中的運行時創建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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM