当前位置:网站首页>亲测可用!!!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
{
//有输入框还未输入内容。。。。。
}
}
这样,不管我们的窗口界面有多少输入框,我们都可以通过以上代码对输入内容进行判断,过滤出不符合要求的输入框,并提示用户。非常好用!!!!
边栏推荐
- ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
- 生信周刊第38期
- Standard SQL/JSON - the sobering part
- am335x 看门狗驱动&看门狗应用例程序
- Comparison of ipv4 and ipv6 (IPV4)
- AWS Amazon cloud account registration, free application for 12 months Amazon cloud server detailed tutorial
- 订song餐系统
- The most complete phpmyadmin vulnerability summary
- Wearing detection and action recognition of protective gear based on pose estimation
- 深度学习基本概念
猜你喜欢

MySQL row-level locks (row locks, adjacent key locks, gap locks)

数据持久化技术——MP

Exploring Plain Vision Transformer Backbones for Object Detection 论文阅读笔记

Character Functions and String Functions

mysql根据多字段分组——group by带两个或多个参数

这款悄然崛起的国产API接口管理工具,你一定要晓得

Android studio connects to MySQL and completes simple login and registration functions

纷享销客罗旭对话元气森林黄晓枫:零售数字化的终点不是创新,而是数据

MySQL百万数据优化总结 一

Experience innovation and iteration through the development of lucky draw mini-programs
随机推荐
使用 Excel 读取 SAP ABAP CDS View 通过 ODBC 暴露出来的数据
Character Functions and String Functions
How to correctly write the binary stream of the file returned by the server to the local file and save it as a file
连续变量离散化教程
mysql根据多字段分组——group by带两个或多个参数
订song餐系统
How MySQL's allowMultiQueries flag relates to JDBC and jOOQ
Chrome开发自定义右键菜单实现快速跳转到指定页面
Docker实践经验:Docker 上部署 mysql8 主从复制
想吃菌子,当然是自己上山找了
A Week of Wonderful Content Sharing (Issue 14)
Android studio connects to MySQL and completes simple login and registration functions
Obsidian设置图床
apisix-Getting Started
列表页优化思路
【Shader】Shader官方示例[通俗易懂]
JVS应用中心
ESP8266-Arduino编程实例-HDC1008温度湿度传感器驱动
R 语言data.frame 中的另一行中减去一行
cesium-Web网页优化进阶