【发布时间】:2017-03-21 20:56:00
【问题描述】:
我有 3 个 stack panels 持有标签,textbox 包含在一个大堆栈面板中,所以它看起来像这样:
<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>
我必须拒绝用户编辑这些字段 txtName、txtLastName 或 txtHeader 以保留空白字段,因此我想循环遍历每个堆栈面板和每个文本框以检查文本是否为空,如果是,我会返回并抛出他弹出一个消息,例如:某些字段是空的,如果我可以准确指定哪些字段,那就太棒了,也许我可以为此使用标签..?
这是我迄今为止尝试过的:
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;
}
}
}
但是使用上面的这段代码,我总是只循环通过容器(那 3 个堆栈面板),我不能每个都向上文本框...
谢谢大家 干杯
【问题讨论】:
标签: wpf loops foreach textbox stackpanel