在TabControl中循环显示Groupbox(Loop through Groupbox in TabControl)

系统教程 行业动态 更新时间:2024-06-14 16:57:39
在TabControl中循环显示Groupbox(Loop through Groupbox in TabControl)

我有一个包含选项卡控件的WPF(第1页,第2页和第3页)。

在标签控件第2页中,我有3个groupbox( groupbox_Agroupbox_Bgroupbox_C ),每个groupbox包含3个文本框

我可以知道循环遍历所有文本框并清除内容的C#代码是什么。

I have a WPF with contain tab control (page1, page2, and page3).

In tab control page 2, I do have 3 groupbox (groupbox_A, groupbox_B, and groupbox_C), and each of the groupbox contain 3 textbox.

May I know what is the C# code to loop through all the textbox and clear the content.

最满意答案

此函数将返回选项卡控件中的所有文本框。

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } } }

您可以通过枚举这样获得所有文本框:

foreach (var textbox in FindVisualChildren<TextBox>(window)) { // do something with tb here }

来源: 按类型查找WPF窗口中的所有控件

This function will return all the textboxes within the tab control.

public static IEnumerable<T> FindVisualChildren<T>(DependencyObject depObj) where T : DependencyObject { if (depObj != null) { for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++) { DependencyObject child = VisualTreeHelper.GetChild(depObj, i); if (child != null && child is T) { yield return (T)child; } foreach (T childOfChild in FindVisualChildren<T>(child)) { yield return childOfChild; } } } }

You can get all text boxes by enumerating like this :

foreach (var textbox in FindVisualChildren<TextBox>(window)) { // do something with tb here }

Source : Find all controls in WPF Window by type

更多推荐

本文发布于:2023-04-13 11:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/9616296debd4ebbede7562f3f601fd28.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:TabControl   Groupbox   Loop

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!