ARTICLE
Listing All Controls in a Windows Store Application
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
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);
}