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

 

相关文章: