WPF C# циклический просмотр элемента управления Tab в поисках текстовых полей
Здравствуйте, у меня проблема. Мне нужно вернуть данные из SQL в окно, чтобы заполнить текстовые поля. Если текстовое поле пусто post return, то поверните цвет фона == красный.
Я заставил его работать, используя приведенный ниже код:
private void PlayingWithColors() { foreach (var tb in FindVisualChildren<TextBox>(this).Where(tb => tb.Text == String.Empty)) { tb.Background = Brushes.Red; } } public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj == null) yield break; for (var i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { var child = VisualTreeHelper.GetChild(depObj, i); var children = child as T; if (children != null) { yield return children; } foreach (var childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } }
Проблема заключается в конечных пользователях. Теперь они хотят иметь меньшее окно и вкладки. Поэтому, как только я добавил tabcontrol, я потерял функциональность. Что я делаю не так или что я могу сделать по-другому, чтобы прочитать вкладки для всех текстовых полей?
Пример XAML:
<TabItem Header="Vitals"> <GroupBox Header="Vital Signs" FontWeight="Bold" BorderBrush="Black" BorderThickness="1" Margin="10"> <StackPanel Margin="10" HorizontalAlignment="Left" Width="782"> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="171" Margin="1,1,1,0" UseLayoutRounding="True" Height="30"> <Label Content="HR: " FontWeight="Normal" HorizontalAlignment="Left" VerticalContentAlignment="Center" Height="27" /> <TextBox x:Name="TbHr" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Width="75" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Height="27" /> </TextBlock> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="171" Margin="1,1,1,0" UseLayoutRounding="True" Height="30"> <Label Content="RR: " FontWeight="Normal" HorizontalAlignment="Left" VerticalContentAlignment="Center" Height="27" /> <TextBox x:Name="TbRr" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Width="75" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Height="27" /> </TextBlock> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="171" Margin="1,1,1,0" UseLayoutRounding="True" Height="30"> <Label Content="BP: " FontWeight="Normal" HorizontalAlignment="Left" VerticalContentAlignment="Center" Height="27" /> <TextBox x:Name="TbBp" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Width="75" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Height="27" /> </TextBlock> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="171" Margin="1,1,1,0" UseLayoutRounding="True" Height="30"> <Label Content="Weight: " FontWeight="Normal" HorizontalAlignment="Left" VerticalContentAlignment="Center" Height="27" /> <TextBox x:Name="TbWgt" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Width="75" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Height="27" /> </TextBlock> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="171" Margin="1,1,1,0" UseLayoutRounding="True" Height="30"> <Label Content="Weight Type: " FontWeight="Normal" HorizontalAlignment="Left" VerticalContentAlignment="Center" Height="27" ToolTip="lb or kg" /> <TextBox x:Name="TbWgtType" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Width="75" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Height="27" /> </TextBlock> <TextBlock HorizontalAlignment="Left" VerticalAlignment="Center" Width="181" Margin="1,1,1,0" UseLayoutRounding="True" Height="30"> <Label Content="Vital Date: " FontWeight="Normal" HorizontalAlignment="Left" VerticalContentAlignment="Center" Height="27" ToolTip="lb or kg" /> <TextBox x:Name="TbVitalDate" TextWrapping="Wrap" Text="" HorizontalAlignment="Center" Width="87" VerticalContentAlignment="Center" HorizontalContentAlignment="Left" Height="27" /> </TextBlock> </StackPanel> </GroupBox> </TabItem>