当前位置:网站首页>亲测可用!!!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
{
//有输入框还未输入内容。。。。。
}
}
这样,不管我们的窗口界面有多少输入框,我们都可以通过以上代码对输入内容进行判断,过滤出不符合要求的输入框,并提示用户。非常好用!!!!
边栏推荐
- 三相PWM整流器预测直接功率控制
- Read through the interface to call the artifact RestTemplate
- R语言做面板panelvar例子
- lotus-local-net 2k v1.17.0-rc4
- If the value of the enum map does not exist, deserialization is not performed
- Detailed tutorial on distributed transaction Seata
- 一文带你了解redux的工作流程——actionreducerstore
- 使用docker搭建mysql主从
- VBA实现双击单元格自动输出对号再次双击取消对号
- SAP Commerce Cloud Product Review 的添加逻辑
猜你喜欢
字符函数和字符串函数
Chrome开发自定义右键菜单实现快速跳转到指定页面
Exploring Plain Vision Transformer Backbones for Object Detection Paper Reading Notes
MySQL row-level locks (row locks, adjacent key locks, gap locks)
kubernetes之服务发现
The latest MySql installation teaching, very detailed
Is the working process of the belt you know the story - actionreducerstore
给你一个大厂面试的机会,你能面试上吗?进来看看!
vb.net 画曲线
Mysql环境变量的配置(详细图解)
随机推荐
JVS应用中心
MySQL日志中“binlog”的三种格式玩起来真爽
MySQL模糊查询性能优化
三相PWM整流器预测直接功率控制
St. Regis Takeaway Project: File Upload and Download
建情人节表白网站(超详细过程,包教包会)
Summary of several defragmentation schemes for MySQL (to solve the problem of not releasing space after deleting a large amount of data)
WebGL给Unity传递参数问题1: Cannot read properties of undefined (reading ‘SendMessage‘)
PAT exam summary (exam experience)
In PLC communication error or timeout or download the prompt solution of the model
栈和队列的基本概念
数据持久化技术——MP
基于C51实现按键控制
Mysql环境变量的配置(详细图解)
SAP 电商云 Spartacus UI 和 Accelerator UI 里的 ASM 模块
数据湖(十九):SQL API 读取Kafka数据实时写入Iceberg表
[core]-ARMV7-A, ARMV8-A, ARMV9-A Architecture Introduction "Recommended Collection"
Docker安装canal、mysql进行简单测试与实现redis和mysql缓存一致性
SAP Commerce Cloud Product Review 的添加逻辑
MySQL row-level locks (row locks, adjacent key locks, gap locks)