WPF 判断Grid控件的子控件不能为空子控件有TextBoxTextBlockComboBox
可以通过遍历Grid控件的子控件,判断每个子控件的类型,再判断其中的TextBox、TextBlock、ComboBox是否为空来判断Grid控件的子控件是否为空。示例代码如下:
bool isGridEmpty = true;
foreach (UIElement element in myGrid.Children)
{
if (element is TextBox)
{
if (!string.IsNullOrEmpty((element as TextBox).Text))
{
isGridEmpty = false;
break;
}
}
else if (element is TextBlock)
{
if (!string.IsNullOrEmpty((element as TextBlock).Text))
{
isGridEmpty = false;
break;
}
}
else if (element is ComboBox)
{
if ((element as ComboBox).SelectedItem != null)
{
isGridEmpty = false;
break;
}
}
}
if (isGridEmpty)
{
// Grid控件的子控件为空
}
else
{
// Grid控件的子控件不为空
}
这段代码首先定义了一个布尔型变量isGridEmpty,初始值为true,表示Grid控件的子控件为空。然后通过foreach循环遍历Grid控件的子控件,判断每个子控件的类型,如果是TextBox、TextBlock或ComboBox,则判断其中的内容是否为空。如果存在任意一个子控件的内容不为空,则将isGridEmpty的值设为false,并退出循环。最后根据isGridEmpty的值判断Grid控件的子控件是否为空
原文地址: http://www.cveoy.top/t/topic/cEpV 著作权归作者所有。请勿转载和采集!