当前位置:网站首页>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();
}
}
边栏推荐
猜你喜欢

解决谷歌浏览器跨域问题has been blocked by CORS policy: The request client is not a secure context and the resou

JS Bom window innerWidth clientWidth onresize 窗口滚动偏移量 返回顶部

Embedded SIG | 分布式软总线

开放地址法哈希实现——二次探测法

复合类型--引用,指针

Oracle 进程数和会话数的关系

【机器学习】通俗易懂决策树(原理篇)

实现批量导出功能

Fudan-Washington University EMBA Kechuang's Ao E丨The Magical Materials and We Are Shaped

Redis(十) - Redission原理与实践
随机推荐
【C语言刷LeetCode】592. 分数加减运算(M)
JUC (5): Problems caused by sharing
复星医药募资44.84亿:高毅资产认购20亿 成第三大股东
ESP8266 +0.96“ I2C OLED 表盘时钟
A plastic bottle of ocean "fantasy drifting"
测试人员,除了测试还得会点什么
实现导入市场活动:
JIT VS AOT
CF1473C No More Inversions
leetcode每天5题-Day01
计划处理链的很多种情况
JUC(七):变量的线程安全分析
Oracle 进程数和会话数的关系
答对这3个面试问题,薪资直涨20K
Awesome, Tencent technical experts handed Redis technical notes, and the download volume has exceeded 30W
A. Strange Birthday Party- Codeforces Round #694 (Div. 1)
22/07/21
B. Different Divisors- Codeforces Round #696 (Div. 2)
影响小程序开发费用的三个因素!
快速入门jsp