当前位置:网站首页>亲测可用!!!WPF中遍历整个窗口的所有TextBox组件,对每个输入框做非空判断。
亲测可用!!!WPF中遍历整个窗口的所有TextBox组件,对每个输入框做非空判断。
2022-07-31 12:07:00 【Monkey_King_GL】
系列文章目录
WPF中TabControl动态获取当前选中的TabItem
WPF中报错:“未将对象引用设置到对象的实例。”
为了提高用户输入数据的合理性,我们通常需要在提交数据之前对输入框内数据进行一些逻辑判断。我相信很多同学会说可以在后台通过控件获取输入信息再判断,如下:
string text = this.TextBox.Text;
if(string.IsNullOrEmpty(text)
{
MessageBox.Show("输入内容不能为空!!");
}
是的,这样确实可以达到效果,但是你们有没有想过,如果一旦你的输入框很多时,你需要写多少个这样的判断语句?十个?百个?这样太麻烦了,并且前端也要进行相应的操作。
通过遍历组件来实现非空限制
首先,我们知道前端的界面其实是一个属性结构,因此我们可以通过遍历这棵树来获取窗口的所有输入框,代码如下:
private List<DependencyObject> controlList = new List<DependencyObject>(); //存储前端所有的TextBox
public void FindInVisualTreeDown(DependencyObject obj, Type type)
{
if (obj != null)
{
//因为我的界面上有TabControl组件,因此我还需要遍历所有的TabItem
if (obj is TabControl)
{
var tab = obj as TabControl;
foreach (DependencyObject item in tab.Items)
{
FindInVisualTreeDown(item,type);
}
}
for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
{
DependencyObject child = VisualTreeHelper.GetChild(obj, i);
if (child.GetType() == type)
{
controlList.Add(child);
}
FindInVisualTreeDown(child, type);
}
}
}
至此,我们已经获取到界面所有的TextBox了,我们就可以通过遍历来对输入框内容进行判断了。
public bool TestEmpty(List<DependencyObject> list)
{
bool flag = true;
foreach (var c in list)
{
if (string.IsNullOrEmpty((c as TextBox).Text))
{
string text_Name = (c as TextBox).Name; //获取组件名称
MessageBox.Show(text_Name + "不能为空!!");
flag = false;
break;
}
}
return flag;
}
private void Button_Click(object sender, RoutedEventArgs e)
{
FindInVisualTreeDown(rootGrid, typeof(TextBox));
if (TestEmpty(controlList))
{
//所有的输入框内容都符合要求。。。。
}
else
{
//有输入框还未输入内容。。。。。
}
}
这样,不管我们的窗口界面有多少输入框,我们都可以通过以上代码对输入内容进行判断,过滤出不符合要求的输入框,并提示用户。非常好用!!!!
边栏推荐
- Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
- After class, watching the documentation and walking back to the lab, I picked up the forgotten SQL operators again
- Docker practical experience: Deploy mysql8 master-slave replication on Docker
- lotus-local-net 2k v1.17.0-rc4
- 关于==和equals的区别和联系,面试这么回答就可以
- 使用docker搭建mysql主从
- MySQL row-level locks (row locks, adjacent key locks, gap locks)
- 初识QEMU
- imx6ull看门狗使用
- Use ODBC in Excel to read data from CDS view on SAP BTP platform
猜你喜欢

The latest MySql installation teaching, very detailed

In PLC communication error or timeout or download the prompt solution of the model
给你一个大厂面试的机会,你能面试上吗?进来看看!

imx6ull看门狗使用

Shengxin Weekly Issue 38

ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动

Experience innovation and iteration through the development of lucky draw mini-programs

After Effects 教程,如何在 After Effects 中修复曝光不足的镜头?

A Week of Wonderful Content Sharing (Issue 14)

mysql根据多字段分组——group by带两个或多个参数
随机推荐
kubernetes之服务发现
Exploring Plain Vision Transformer Backbones for Object Detection Paper Reading Notes
一文吃透接口调用神器RestTemplate
基于姿态估计的护具佩戴检测与动作识别
SAP Commerce Cloud Product Review 的添加逻辑
MySQL日志中“binlog”的三种格式玩起来真爽
使用docker搭建mysql主从
MySQL模糊查询性能优化
Character Functions and String Functions
Experience innovation and iteration through the development of lucky draw mini-programs
mysql根据多字段分组——group by带两个或多个参数
Is the working process of the belt you know the story - actionreducerstore
【Shader】Shader官方示例[通俗易懂]
列表页优化思路
dosbox基础使用[通俗易懂]
Candence学习篇(11) allegro中设置规则,布局,走线,铺铜
基于稳态视觉诱发电位和注意力脑电的混合脑机接口系统
Read through the interface to call the artifact RestTemplate
Qt鼠标穿透
关于Mysql数据库的介绍