【问题标题】:How to loop through stack panels which are contained in one stack panel如何遍历包含在一个堆栈面板中的堆栈面板
【发布时间】: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


    【解决方案1】:

    您可以使用以下辅助方法在可视化树中查找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;
        }
    }
    

    【讨论】:

    • 很高兴再次见到您! :)) 非常感谢 :)
    【解决方案2】:

    stackControls 堆栈面板的 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;
                }
            }
        }
    }
    

    【讨论】:

    • 我在发布答案之前试过这个,我得到错误'object' does not contain a definition for 'Children' and no extension method 'Children'接受'object'类型的第一个参数(您是否缺少 using 指令或程序集引用?)在第二个 foreach 循环中...
    • 对不起,我忘记了类型转换。我已经更正了示例。
    猜你喜欢
    • 1970-01-01
    • 2011-07-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多