动态地将内容添加到中心部分 XAML
本文关键字:心部 XAML 添加 动态 | 更新日期: 2023-09-27 18:27:16
XmlReader tXml = XmlReader.Create(new StringReader("certain xaml code which has the desired layout..."));
UIElement MyElement = (UIElement) XamlReader.Load(tXml.ToString());
我需要在 hubsection 内动态添加运行时创建的MyElement
。 我怎样才能做到这一点?
谢谢!
您可以在数据模板标记之间插入 xaml 字符串:
var myXaml = "<TextBlock>test</TextBlock>";
var template = XamlReader.Load("<DataTemplate xmlns='"http://schemas.microsoft.com/winfx/2006/xaml/presentation'">" + myXaml + "</DataTemplate>");
如果需要,不要忘记声明默认命名空间和其他命名空间。
接下来,您只需要设置HubSection的内容模板属性:
Section1.ContentTemplate = template as DataTemplate;
假设您的中心声明如下:
<Hub>
<HubSection x:Name="Section1" />
</Hub>