WPF C#通过Tab Control循环查找Textboxes [英] WPF C# Looping through Tab Control looking for Textboxes

查看:119
本文介绍了WPF C#通过Tab Control循环查找Textboxes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有问题。我需要将数据从SQL返回到一个Window来填充文本框。如果返回后文本框为空,则将背景颜色= =红色。



我使用下面的代码让它工作:



Hello, I have a problem. I need to return data from SQL into a Window to fill in textboxes. If a textbox is empty post return, then turn the background color == red.

I got it to work using the code below:

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:





The problem is the end users. They now want a smaller window and tabs. So as soon as I added the tabcontrol, I lost the functionality. What am I doing wrong or what can I do differently to read through the tabs for all textboxes?

Example 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>

推荐答案

问题在于迭代Visual Tree以查找控件。如果您调试应用程序,您可以看到显示不同于当前的tabitem的TextBox控件是不可见的,因此不会显示在Visual Tree中。



只需将FindVisualChildren方法替换为



Issue is with iterating over Visual Tree to find control. If you debug your application you can see that TextBox Control that is present different tabitem than current is not visible and hence not shown in Visual Tree.

Just replace FindVisualChildren method with

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject obj) where T : DependencyObject
       {
           if (obj != null)
           {
               if (obj is T)
                   yield return obj as T;

               foreach (DependencyObject child in LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>())
                   foreach (T c in FindVisualChildren<T>(child))
                       yield return c;
           }
       }







希望这能解决您的问题。




Hope this will solve your problem.


基于上面的解决方案,我调整了代码,以便您可以在带有选项卡控件或其他容器的WPF窗口中进行调整,并且它将遍历所有文本框控件。 TB控件被添加到可视项目列表中,使用返回的列表,您可以执行任何您喜欢的操作。



删除where子句以删除仅限空文本框的限制。 (父母)之后别忘了关闭支架。



删除此==> .Where(tb => tb.Text == String.Empty))



Based on the above solution, I tweaked the code so you can pas in a WPF window with tab controls or other containers, and it will loop through all the textbox controls. The TB controls are added to a visual items list and with the returned list you can do whatever you like.

Remove the where clause to remove the restriction for only empty textboxes. Don't forget to close the bracket after (parent) )

Remove this ==> .Where(tb => tb.Text == String.Empty))

<pre>public class cEnumControls
    {
        public static List<Visual> EnumVisual(UIElement Parent, List<Visual> collection)
        {
            foreach (var tb in FindVisualChildren<TextBox>(Parent).Where(tb => tb.Text == String.Empty))
            {
                collection.Add((Visual)tb);
                //tb.Background = Brushes.Red;
            }
            return collection;
        }


        public static IEnumerable<T> FindVisualChildren<T>(DependencyObject obj) where T : DependencyObject
        {
            if (obj != null)
            {
                if (obj is T)
                    yield return obj as T;

                foreach (DependencyObject child in LogicalTreeHelper.GetChildren(obj).OfType<DependencyObject>())
                    foreach (T c in FindVisualChildren<T>(child))
                        yield return c;
            }
        }
    }


这篇关于WPF C#通过Tab Control循环查找Textboxes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆