在TabControl中循环显示Groupbox

时间:2016-07-28 08:21:58

标签: c# wpf

我有一个包含标签控件的WPF(第1页,第2页和第3页)。

在标签控件第2页中,我有3个组框( groupbox_A groupbox_B groupbox_C ),并且每个组框都包含 3文本框

我可以知道在所有文本框中循环并清除内容的C#代码是什么。

2 个答案:

答案 0 :(得分:0)

此函数将返回选项卡控件中的所有文本框。

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject
{
    if (depObj != null)
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
            if (child != null && child is T)
            {
                yield return (T)child;
            }

            foreach (T childOfChild in FindVisualChildren<T>(child))
            {
                yield return childOfChild;
            }
        }
    }
}

您可以通过以下列举来获取所有文本框:

foreach (var textbox in FindVisualChildren<TextBox>(window))
{
    // do something with tb here
}

来源:Find all controls in WPF Window by type

答案 1 :(得分:0)

这看起来不是一个好方法..找到另一个逻辑来清除你的文本......但如果你只是想知道它是如何工作的......

最简单的方法:

IEnumerable<myType> collection = control.Children.OfType<myType>();

(控件是窗口的根元素,myType在你的情况下是groubBox)

通过所有元素Link的微软方式:

 // Enumerate all the descendants of the visual object.
 static public void EnumVisual(Visual myVisual)
 {
     for (int i = 0; i < VisualTreeHelper.GetChildrenCount(myVisual); i++)
     {
        // Retrieve child visual at specified index value.
        Visual childVisual = (Visual)VisualTreeHelper.GetChild(myVisual, i);

        // Do processing of the child visual object.

        // Enumerate children of the child visual object.
        EnumVisual(childVisual);
    }
 }