Reader Level:
Article

Listing All Controls in a Windows Store Application

By Ibrahim Ersoy on Nov 17, 2012
If you want to iterate through all the available controls in your application, you should read this article.

Hi,

I was building an application for Wowzapp and encountered a problem that got me thinking about for an hour or two. My idea was to list all the button controls so I could check their values for further development and here is my solution.

If you want to iterate all the controls in a grid-like structure, you should try this code:
 

int count = VisualTreeHelper.GetChildrenCount(grid1);
for (int i = 0; i < count; i++)
{
   	FrameworkElement childVisual = (FrameworkElement)VisualTreeHelper.GetChild(grid1, i);
   	Debug.WriteLine(childVisual.Name);
}

This code lists all the controls with name properties set on.

But if you're looking for listing a specific type of control then use this code:


int count = VisualTreeHelper.GetChildrenCount(grid1);
for (int i = 0; i < count; i++)
{
    FrameworkElement childVisual = (FrameworkElement)VisualTreeHelper.GetChild(grid1, i);
    if(childVisual is Button)
    {
        Debug.WriteLine(childVisual.Name);
    } }

By doing so, now you'll be listing all the Button controls!

Hope this helps!

Article Extensions
Contents added by Mahesh Chand on Nov 18, 2012

List All Controls

To add one more line to this article, if you remove the following "if" statement, you can load all controls.

  if(childVisual is Button)
       {
          Debug.WriteLine(childVisual.Name);
       }
Ibrahim Ersoy

Ibrahim Ersoy lives in Turkey, Istanbul. He helps running and managing C# Corner. He is a Software Consultant in Sharepoint field,completed over 18 Intranet,Extranet,Internet wide SharePoint portal projects.He is also 2 ... Read more

Personal Blog: http://www.iersoy.com

COMMENT USING