当前位置:网站首页>WPF递归获取窗体中指定控件类型列表
WPF递归获取窗体中指定控件类型列表
2022-07-30 02:58:00 【太阳风暴】
导言:记录WPF递归获取窗体中指定控件类型列表
在下面的代码我主要是对 TextBox 批量撤销、清除数据等操作
一、代码片段
- 前端代码
<Window x:Class="xx_MSS.Pages.View.InstrumentPages.InstrumentConnectConfigWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="d" Title="联机配置" Height="370" Width="370">
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="63*"/>
<ColumnDefinition Width="118*"/>
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="Auto"/>
<RowDefinition/>
<RowDefinition/>
<RowDefinition Height="2*"/>
</Grid.RowDefinitions>
<TextBlock Text="仪 器 IP:" Grid.Row="0" Grid.Column="0" Height="18" Margin="26,25,0,25" Width="99"/>
<TextBox Grid.Row="0" Grid.Column="1" />
<TextBlock Text="端 口:" Grid.Row="1" Grid.Column="0" Height="18" Width="95"/>
<TextBox Grid.Row="1" Grid.Column="1" />
<TextBlock Text="超时设置(秒):" Grid.Row="2" Grid.Column="0" Height="18" />
<TextBox Grid.Row="2" Grid.Column="1" />
<Grid Grid.Row="3" Grid.Column="1">
<Grid.ColumnDefinitions>
<ColumnDefinition/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<Button Content="取消" Click="ConfigCancel" Grid.Column="0"/>
<Button Content="确定" Click="ConfigYes" Grid.Column="1"/>
</Grid>
</Grid>
</pu:WindowX>
- 后端代码
using Panuon.UI.Silver;
using System.Windows;
using System.Windows.Controls;
namespace xx_MSS.Pages.View.InstrumentPages
{
/// <summary>
/// InstrumentConnectConfigWindow.xaml 的交互逻辑
/// </summary>
public partial class InstrumentConnectConfigWindow : WindowX
{
public InstrumentConnectConfigWindow()
{
InitializeComponent();
}
private void ConfigCancel(object sender, System.Windows.RoutedEventArgs e)
{
Grid gridPanel = this.Content as Grid;
UIElementCollection UiChildren = gridPanel.Children;
foreach (UIElement item in UiChildren )
{
if (item is TextBox)
{
TextBox textBox = item as TextBox;
//对每一个输入框批量操作
textBox.Undo();
}
}
}
private void ConfigYes(object sender, RoutedEventArgs e)
{
this.Close();
}
}
}
二、核心代码
//这里需要是布局器内部的元素-可以用foreach 也可以用for循环
Grid gridPanel = this.Content as Grid;
UIElementCollection UiChildren = gridPanel.Children;
foreach (UIElement item in UiChildren )
{
if (item is TextBox)
{
TextBox textBox = item as TextBox;
//对每一个输入框批量操作
textBox.Undo();
}
}
//for循环版本
Grid gridPanel = this.Content as Grid;
UIElementCollection UiChildren = gridPanel.Children;
for(int i = 0; i< UiChildren.Count; i++)
{
if (UiChildren[i] is TextBox)
{
TextBox textBox = UiChildren[i] as TextBox;
//对每一个输入框批量操作
textBox.Undo();
}
}
边栏推荐
- 判断Object是否依赖于名叫“XX“的资产
- 博客搭建十:hugo博客添加友链
- OPENSQL
- Embedded SIG | 分布式软总线
- 【GPU并行计算】利用OpenCL&OpenCLUtilty进行GPU并行计算
- (RCE)远程代码/命令执行漏洞漏洞练习
- 复合类型--引用,指针
- JS history.back() go(-1) Location 跳转 重新加载页面 get请求 返回顶部 bom
- 阿里云EasyNLP中文文图生成模型带你秒变艺术家
- 解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou
猜你喜欢
随机推荐
houdini 使用HDA Processor 实现处理HDA输入输出
A plastic bottle of ocean "fantasy drifting"
Leetcode.19 删链表倒数第 N 个结点(栈/先后指针)
VMware磁盘扩容记录
The relationship between the number of Oracle processes and the number of sessions
使用SqlSessionFactory工具类抽取
HCIP实验(05)OSPF综合实验
应用在光伏逆变器中的IGBT晶圆
Oracle 进程数和会话数的关系
重写并自定义依赖的原生的Bean方法
机器学习1一回归模型(一)
【服务器存储数据恢复】华为OceanStor某型号存储raid5数据恢复案例
matlab之函数
生死时速,分秒必争
un7.29:Linux——centos中如何安装与配置redis?
JS Navigator appName appVersion userAgent platform
最重要的传输层
Ansible简介(详细)特性+优点+设计理念+应用领域+系统架构+工作原理+任务执行流程
运营人必须掌握的6大类26个基本模型
leetcode每天5题-Day01