您的位置:首页 > 其它

Silverlight动态创建XAML对象和遍历对象

2012-09-03 22:10 190 查看
1.动态创建XAML对象

StringBuilder xaml = new StringBuilder();

xaml.Append("<TextBlock ");
xaml.Append("xmlns=\"http://schemas.microsoft.com/client/2007\" ");
xaml.Append(" FontSize=\"50\"");
xaml.Append(" FontWeight=\"Bold\" Text=\"动态创建XAML对象\"/>");
//创建textBlock对象
TextBlock textBlock =
(TextBlock)XamlReader.Load(xaml.ToString());
//添加TextBlock到parentCanvas
LayoutRoot.Children.Add(textBlock);

2.遍历对象

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(LayoutRoot); i++)
{
var Child = VisualTreeHelper.GetChild(LayoutRoot, i);
if (Child is TextBox)
{
MessageBox.Show(((TextBox)Child).Name);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: