foreach 语句不能对“System.Windows.Controls.GroupBox”类型的变量进行操作

发布于 2024-11-27 12:45:07 字数 904 浏览 0 评论 0原文

foreach 语句不能对类型的变量进行操作 'System.Windows.Controls.GroupBox' 因为 “System.Windows.Controls.GroupBox”不包含公共 “GetEnumerator”的定义

mycode 的定义:

    foreach (var txt in this.groupBox1.Children)
        {
            if (txt is TextBox)
            {
                (txt as TextBox).Text = string.Empty;
            }
        }

但是为什么网格的代码是正确的?

    foreach (var txt in this.MyGrid.Children)
    {
        if (txt is TextBox)
        {
            (txt as TextBox).Text = string.Empty;
        }
    }

groupBox 的正确代码是什么?

/////////////////编辑

正确代码:

    foreach (var txt in this.MyGridInGroupBox.Children)
    {
        if (txt is TextBox)
        {
            (txt as TextBox).Text = string.Empty;
        }
    }

foreach statement cannot operate on variables of type
'System.Windows.Controls.GroupBox' because
'System.Windows.Controls.GroupBox' does not contain a public
definition for 'GetEnumerator'

mycode :

    foreach (var txt in this.groupBox1.Children)
        {
            if (txt is TextBox)
            {
                (txt as TextBox).Text = string.Empty;
            }
        }

But why is the correct code for the Grid ?

    foreach (var txt in this.MyGrid.Children)
    {
        if (txt is TextBox)
        {
            (txt as TextBox).Text = string.Empty;
        }
    }

What is the correct code for groupBox?

/////////////////editing

Correct code:

    foreach (var txt in this.MyGridInGroupBox.Children)
    {
        if (txt is TextBox)
        {
            (txt as TextBox).Text = string.Empty;
        }
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

我的影子我的梦 2024-12-04 12:45:07

您的第一个代码段甚至无法编译(假设 groupBox1 确实是 GroupBox),因为 GroupBox 没有 Children财产。

GroupBox 只能包含一个子项,由其 Content 属性表示。

如果您需要迭代 GroupBox 的所有可视子级,则可以使用 VisualTreeHelper 类。像这样的东西:

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(groupBox1); i++)
{
    var txt = VisualTreeHelper.GetChild(groupBox1, i);
    if (txt is TextBox) ...
}

更新

好吧,你是说这不起作用,我想我明白为什么。

VisualTreeHelper 只会查找 GroupBox 的第一级可视子级,它(在实现控件时)是一个 Grid

这对您没有好处,因为您需要递归到控件的子级并找到所有文本框。

在这种情况下,您最好使用网络上众多递归“FindChildren”实现之一。这是我的一个:

public static class DependencyObjectExtensions
{
    public static IEnumerable<T> GetVisualChildren<T>(this DependencyObject depObj) 
        where T : DependencyObject
    {
        if (depObj == null) yield break;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var t = child as T;
            if (t != null) yield return t;

            foreach (var item in GetVisualChildren<T>(child))
            {
                yield return item;
            }
        }
    }
}

你可以像这样使用它:

foreach (var txt in groupBox1.GetVisualChildren<TextBox>())
{
    txt.Text = String.Empty;
}

Your first snippet won't even compile (assuming groupBox1 is indeed a GroupBox), since GroupBox has no Children property.

A GroupBox can only contain one child, represented by its Content property.

If you need to iterate over all the visual children of a GroupBox, you might be able to use the VisualTreeHelper class. Something like this:

for (int i = 0; i < VisualTreeHelper.GetChildrenCount(groupBox1); i++)
{
    var txt = VisualTreeHelper.GetChild(groupBox1, i);
    if (txt is TextBox) ...
}

Update

Ok, you're saying that this doesn't work, and I think I understand why.

The VisualTreeHelper will only find the first-level visual children of the GroupBox, which (as the control is implemented) is a Grid.

This is no good to you, because you need to recurse down into the children of the control and find all the TextBoxes.

In that case, you're better off using one of the many recursve "FindChildren" implementations around the web. Here's one of mine:

public static class DependencyObjectExtensions
{
    public static IEnumerable<T> GetVisualChildren<T>(this DependencyObject depObj) 
        where T : DependencyObject
    {
        if (depObj == null) yield break;

        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
        {
            var child = VisualTreeHelper.GetChild(depObj, i);

            var t = child as T;
            if (t != null) yield return t;

            foreach (var item in GetVisualChildren<T>(child))
            {
                yield return item;
            }
        }
    }
}

You can use that like this:

foreach (var txt in groupBox1.GetVisualChildren<TextBox>())
{
    txt.Text = String.Empty;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文