还是因为要用中文,所以需要遍历所有的TextBlock,一一设置字体……还好,不算复杂,先找根Canvas中的节点,如果是TextBlock就加入到List中,如果是Canvas的话就递归一次,然后把返回结果也加入到List中,最终返回一个TextBlock数组。

TextBlock[] FindAllTextBlock(Canvas container)
{
List tbl = new List();

for (int i = 0; i < container.Children.Count; i++)
{
//如果是TextBlock,就加入到List中
if (container.Children[i].GetType() == typeof(TextBlock))
tbl.Add(container.Children[i] as TextBlock);
//如果是Canvas,就继续递归查找
else if (container.Children[i].GetType() == typeof(Canvas))
tbl.AddRange(FindAllTextBlock(container.Children[i] as Canvas));
}

return tbl.ToArray();
}

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.