如何访问PhoneApplicationPage中的所有元素?

时间:2012-05-26 11:44:00

标签: c# .net xaml windows-phone-7.1

我想知道如何访问从IEnumerable派生的页面中的所有元素PhoneApplicationPage

如果是ASP.Net WebForms,它将如下所示:

    foreach (Control control in this.Controls)
    {
        //Do something
    }

但是在Windows Phone 7.5中找不到相应的内容!

1 个答案:

答案 0 :(得分:1)

您可以使用VisualTreeHelper

来实现此目的

做类似的事情:

LoopThroughControls(LayoutRoot);

private void LoopThroughControls(UIElement parent)
{
  int count = VisualTreeHelper.GetChildrenCount(parent);
  if (count > 0)
  {
    for (int i = 0; i < count; i++)
    {
      UIElement child = (UIElement)VisualTreeHelper.GetChild(parent, i);
      string childTypeName = child.GetType().ToString();
      LoopThroughControls(child);
    }
  }
}