How to get list of controls in a group box in WPF
In standard WinForms development, I would do the following:
foreach (Control in groupBox1.Controls)
{
MessageBox.Show(c.Name);
}
How does a guy do it in WPF? I have a Grid inside a GroupBox and several controls in the grid (buttons, etc.), but cannot figure out how to get each control.
source to share
As MSDN advises, you will need to iterate over the controls as children GroupBox
. Also note that you usually need to add Grid
to yours GroupBox
to add new controls to this GroupBox
. Thus, you will need to get the children Grid
in this, GroupBox
and iterate through something like this:
//iterate through the child controls of "grid"
int count = VisualTreeHelper.GetChildrenCount(grid);
for (int i = 0; i < count; i++)
{
Visual childVisual = (Visual)VisualTreeHelper.GetChild(grid, i);
if (childVisual is TextBox)
{
//write some logic code
}
else
{
}
}
You may find this useful: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93ebca8f-2977-4c36-b437-9c82a22266f6
source to share