如何遍历一个堆栈面板中包含的堆栈面板
How to loop through stack panels which are contained in one stack panel
我有 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 中的任何一个以留下空白字段,所以我想循环遍历每个堆栈面板和每个文本框以检查文本是否为空,如果是的话我会 return 并向他弹出一个消息,如:有些字段是空的,如果我能准确指定哪些字段,那就太棒了,也许我可以为此使用标签..?
这是我目前尝试过的方法:
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 个堆栈面板),我不能每个都打开文本框...
谢谢大家
干杯
stackControls
堆栈面板的 Children 属性 returns 只有它的
立即 children,即三个堆栈面板。所以你应该使用嵌套循环来获取文本框:
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;
}
}
}
}
您可以使用以下辅助方法在可视化树中查找 StackPanel
的所有 TextBox
个子项:
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;
}
}
我有 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 中的任何一个以留下空白字段,所以我想循环遍历每个堆栈面板和每个文本框以检查文本是否为空,如果是的话我会 return 并向他弹出一个消息,如:有些字段是空的,如果我能准确指定哪些字段,那就太棒了,也许我可以为此使用标签..?
这是我目前尝试过的方法:
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 个堆栈面板),我不能每个都打开文本框...
谢谢大家 干杯
stackControls
堆栈面板的 Children 属性 returns 只有它的
立即 children,即三个堆栈面板。所以你应该使用嵌套循环来获取文本框:
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;
}
}
}
}
您可以使用以下辅助方法在可视化树中查找 StackPanel
的所有 TextBox
个子项:
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;
}
}