views:

1081

answers:

2

In standard WinForms development I would do the following:

foreach (Control in groupBox1.Controls)
{
     MessageBox.Show(c.Name);
}

How does a guy do this in WPF? I have a Grid inside the GroupBox and a number of controls in the grid (buttons etc.) but can't seem to figure out how to get each control.

A: 

Instead of .Controls, you'll be looking for the .Children property.

Additionally, that will only return first order children. You'll want to recursively find all children of all controls if you truly want all descendants of the GroupBox.

JustLoren
+1  A: 

As MSDN advises, you will need to iterate the controls as children of the GroupBox. Also, note that you usually need to add a Grid into your GroupBox to be able to add new controls into that GroupBox. So you will need to get the children of the Grid in that GroupBox and iterate through them, someething 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 might find this useful: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93ebca8f-2977-4c36-b437-9c82a22266f6

Derar
I should've used Visual instead of Control to be more accurate..
Derar