Amir
Amir

Reputation: 9627

How to access all the elements within PhoneApplicationPage?

I'm wondering how can I access an IEnumerable of all the elements in a page derived from PhoneApplicationPage?

If it was ASP.Net WebForms, it would be something like below:

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

But cannot find the equivalent in Windows Phone 7.5!

Upvotes: 0

Views: 552

Answers (1)

Magnus Johansson
Magnus Johansson

Reputation: 28325

You can probably achieve this using the VisualTreeHelper class

Do something like:

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

Upvotes: 1

Related Questions