Cycle through all children in parent element

Preveen 216 Reputation points
2020-06-17T22:26:57.03+00:00

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.

Universal Windows Platform (UWP)
0 comments No comments
{count} votes

Accepted answer
  1. Roy Li - MSFT 32,731 Reputation points Microsoft Vendor
    2020-06-18T06:37:34.353+00:00

    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 additional answer

Sort by: Most helpful
  1. Daniele 1,996 Reputation points
    2020-06-18T06:15:06.487+00:00

    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;
    
    0 comments No comments