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

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);
    }
}

 

posted @ 2012-09-03 22:10  飛雲若雪  阅读(579)  评论(0编辑  收藏  举报