2015-12-17 140 views
1
XmlReader tXml = XmlReader.Create(new StringReader("certain xaml code which has the desired layout...")); 
UIElement MyElement = (UIElement) XamlReader.Load(tXml.ToString()); 

我需要在hubsection中动态添加在运行时创建的MyElement。我怎么能做到这一点?动态添加内容到hubsection xaml

谢谢!

回答

0

您可以将您的字符串的XAML一个DataTemplate标签之间:

var myXaml = "<TextBlock>test</TextBlock>"; 
var template = XamlReader.Load("<DataTemplate xmlns=\"http://schemas.microsoft.com/winfx/2006/xaml/presentation\">" + myXaml + "</DataTemplate>"); 

如果需要的话,不要忘记申报默认命名空间等。

接下来,你只需要设置你的ContentTemplate属性HubSection:

Section1.ContentTemplate = template as DataTemplate; 

假设集线器声明如下:

<Hub> 
    <HubSection x:Name="Section1" /> 
</Hub>