可以使用以下代码判断 control 的 Text 是否为空:

foreach (var t in Panel.Children)
{
    if (t is Control control)
    {
        if (string.IsNullOrEmpty(control.Text))
        {
            // Text为空
        }
        else
        {
            // Text不为空
        }
    }
}

该代码首先使用 foreach 循环遍历 Panel.Children 集合,然后使用 is 运算符判断每个元素是否为 Control 类型。如果是,则将该元素转换为 Control 类型并赋值给 control 变量。最后,使用 string.IsNullOrEmpty() 方法判断 control.Text 属性是否为空。

注意:

  • 如果 Control 的 Text 属性为 null 或空字符串,则 string.IsNullOrEmpty() 方法返回 true。
  • 如果 Control 的 Text 属性不为空,则 string.IsNullOrEmpty() 方法返回 false。

您可以根据需要修改代码中的注释,以执行不同的操作。

WPF 判断 Control 的 Text 是否为空

原文地址: https://www.cveoy.top/t/topic/j45l 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录