question

donpro2009 avatar image
0 Votes"
donpro2009 asked donpro2009 commented

Cycle through all children in parent element

Hi,
How do I cycle through all child controls in a parent control and set a certain property?

For example:
Parent element: StackPanel
Child elements: Buttons, TextBoxes, ComboBoxes and other StackPanels (which contain aforementioned controls)

Property to set: IsEnabled property

Thanks in advance.

windows-uwp
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

RoyLi-MSFT avatar image
0 Votes"
RoyLi-MSFT answered donpro2009 commented

Hello,


Welcome to Microsoft Q&A!

Based on your words, you only have panels and controls in your code. IsEnabled property only exists in elements inherited from Control Object. Panels like StackPanel and Grid don't have such a property. So you need to find out if an element is a control or a panel. You could use Panel.Children Property to get all the children objects of the panel and try to find all the controls.

Here is the code that you could use:

 private void MyButton_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
     {
         //MyPanel is the target panel.
         foreach (UIElement element in MyPanel.Children) 
         {
             FindControl(element);
         }
     }

     void FindControl(UIElement uiElement) 
     {
         //if it is a panel
         if (uiElement is Panel)
         {
             Panel panel = uiElement as Panel;
             foreach (UIElement element in panel.Children)
             {
                 //call the method itself again.
                 FindControl(element);
             }
         }
         else if (uiElement is Control) 
         {
             //if it is a control, set the isenable property.
             Control control = uiElement as Control;
             control.IsEnabled = false;
         }

     }


Thank you.


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Thank you!

This is exactly what I wanted.

Thanks also for working it out in such detail 🙂.

Cheers! Stay safe.

0 Votes 0 ·
danielescipioni avatar image
0 Votes"
danielescipioni answered danielescipioni edited

I see 2 way to set IsEnabled property:

  • use the following method to change IsEnabled on all child controls

private void ChangeIsEnabled(Panel panel)
{
    foreach (UIElement child in panel.Children)
    {
        if (child is Panel childPanel)
        {
            ChangeIsEnabled(childPanel);
        }
        else if (child is Control control)
        {
            control.IsEnabled = !control.IsEnabled;
        }
    }
}


if your stack panel has x:Name property set, for example

<StackPanel x:Name="MyStackPanel" >
    ...
</StackPanel>


that is the usage

ChangeIsEnabled(MyStackPanel)

  • wrap all relevant controls in a ContentControl

<ContentControl Grid.Row="1" x:Name="MyContentControl">
    <StackPanel>
        ...
    </StackPanel>
</ContentControl>


and use this code

MyContentControl.IsEnabled = !MyContentControl.IsEnabled;



5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.