-2

I'm trying to iterate forms in wpf but seems that

foreach (Control x in this.Controls) 
 { 
    if (x is TextBox) 
    {    
      do something
    } 
 }

is not working! compiler doesn't recognize this.controls ....seems there is a difference in wpf but I don't what code should I use instead! Edit: I don't wanna use FindVisualChildren

M.B
  • 23
  • 4
  • and has more shortcut answers so This is not Duplicate :) is there any way to get it out of this dupicated error? – M.B Mar 14 '19 at 15:21

1 Answers1

0

Try something like this:

        foreach (var control in MyGrid.Children.OfType<TextBox>())
        {
                //do something
        }

You cannot iterate through all your controls in a Window. You need to be more specific. Please note that this will only get direct children and not children of children

<Window>
   <Grid x:Name="MyGrid>
     <Button/>
     <TextBox/>
     <Label/>
   </Grid>
</Window>
Denis Schaf
  • 2,478
  • 1
  • 8
  • 17