1

I have 3 stack panels which are holding label and textbox contained in one big stack panel, so it looks like this:

 <StackPanel Name="stackControls" Grid.Row="1" Orientation="Vertical" VerticalAlignment="Center" HorizontalAlignment="Center">

        <StackPanel HorizontalAlignment="Right" Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Center">
           <Label x:Name="lblName" Content="Broj kase:" Foreground="Black" FontSize="14" VerticalAlignment="Center"/>
           <TextBox Name="txtName" VerticalContentAlignment="Center" FontSize="15"  Foreground="Black" FontFamily="Arial" Style="{StaticResource TextBoxStyle1}" Width="500" Height="45" />
        </StackPanel>

        <StackPanel HorizontalAlignment="Right" Margin="0,5,0,0" Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Center">
          <Label x:Name="lblLastName" Content="Generični printer:" Foreground="Black" FontSize="14" VerticalAlignment="Center"/>
          <TextBox Name="txtLastName" VerticalContentAlignment="Center" FontSize="15"  FontFamily="Arial" Width="500" Style="{StaticResource TextBoxStyle1}" Height="45" />
        </StackPanel>

        <StackPanel HorizontalAlignment="Right" Margin="0,5,0,0" Orientation="Horizontal" Grid.Row="1" VerticalAlignment="Center">
          <Label x:Name="lblHeader" Content="Veličina naslova na gridu:" Foreground="Black" FontSize="14" VerticalAlignment="Center"/>
          <TextBox Name="txtHeader" VerticalContentAlignment="Center" FontSize="15"  FontFamily="Arial" Width="500" Style="{StaticResource TextBoxStyle1}" Height="45" />
        </StackPanel>

</StackPanel>

I must deny user on edit of any of this fields txtName, txtLastName, or txtHeader to leave blank field, so I wanted to loop through each stackpanel and through each textbox to check is text empty, if yes I would return and throw him a popup with message like : some of fields are empty, it would be awesome if I could specify which fields exactly, maybe I could use tags for that..?

This is what I've tried so far:

 foreach (var c in this.stackControls.Children)
 {

    if (c is StackPanel)
    {

       TextBox textBox = c as TextBox;
       if (String.IsNullOrEmpty(textBox.Text))
       {
           MessageBox.Show("Some fields are empty.",
                            "Edit", 
                            MessageBoxButton.OK,
                            MessageBoxImage.Information);
           return;
       }
     }
   }

But with this code above I would always loop only through containers ( those 3 stack panels) and I couldn't each up textboxes...

Thanks guys Cheers

Walt Ritscher
  • 6,977
  • 1
  • 28
  • 35
Roxy'Pro
  • 4,216
  • 9
  • 40
  • 102

2 Answers2

1

You could use the following helper method to find all TextBox children of the StackPanel in the visual tree:

Find all controls in WPF Window by type

foreach (var textBox in FindVisualChildren<TextBox>(stackControls))
{
    if (String.IsNullOrEmpty(textBox.Text))
    {
        MessageBox.Show(textBox.Name + " is empty.",
                            "Edit",
                            MessageBoxButton.OK,
                            MessageBoxImage.Information);
        return;
    }
}
Community
  • 1
  • 1
mm8
  • 163,881
  • 10
  • 57
  • 88
0

The Children property of the stackControls stack panel returns only its
immediate children, i.e. three stack panels. So you should use the nested loop to get the text boxes:

foreach (var childPanel in this.stackControls.Children)
{
    if (childPanel is StackPanel)
    {
       foreach (var c in ((StackPanel)childPanel).Children)
       {
            TextBox textBox = c as TextBox;
            if (String.IsNullOrEmpty(textBox.Text))
            {
                MessageBox.Show("Some fields are empty.",
                            "Edit", 
                            MessageBoxButton.OK,
                            MessageBoxImage.Information);
                return;
            }
        }
    }
}
Pavel
  • 910
  • 5
  • 6
  • I tried this before posting answer, and I got error 'object' does not contain a definition for 'Children' and no extension method 'Children' accepting a first argument of type 'object' could be found (are you missing a using directive or an assembly reference?) on second foreach loop... – Roxy'Pro Mar 21 '17 at 21:39
  • Sorry, I forgot the type cast. I've corrected the sample. – Pavel Mar 21 '17 at 21:44