WPF 判断Grid控件的子控件不能为空子控件有TextBoxTextBlockComboBox只需要判断Control内容控件是否为空
可以通过遍历Grid控件的子控件,判断子控件类型是否为TextBox、TextBlock或ComboBox,再判断其内容是否为空来判断Grid控件的子控件是否为空。以下是示例代码:
bool IsGridEmpty(Grid grid)
{
foreach (var child in grid.Children)
{
if (child is TextBox textBox && string.IsNullOrEmpty(textBox.Text))
{
return true;
}
else if (child is TextBlock textBlock && string.IsNullOrEmpty(textBlock.Text))
{
return true;
}
else if (child is ComboBox comboBox && comboBox.SelectedItem == null)
{
return true;
}
}
return false;
}
该方法接受一个Grid控件作为参数,返回一个bool值,表示该Grid控件的子控件是否为空。遍历Grid控件的子控件,对每个子控件进行类型判断,如果是TextBox、TextBlock或ComboBox,则判断其内容是否为空,如果为空则返回true。如果遍历完所有子控件后都没有返回true,则说明Grid控件的子控件都不为空,返回false
原文地址: https://www.cveoy.top/t/topic/cEsM 著作权归作者所有。请勿转载和采集!