返回指定Canvas中的所有TextBlock

Posted on September 04, 2007 by Fdream

还是因为要用中文,所以需要遍历所有的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();
}

分享 |
Categories:
Silverlight/C#
Tags:
Comments:
Leave a comment
Views:
18,988 Views

Related Posts

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>