在TabControl中循环显示Groupbox(Loop through Groupbox in TabControl)
我有一个包含选项卡控件的WPF(第1页,第2页和第3页)。
在标签控件第2页中,我有3个groupbox( groupbox_A , groupbox_B和groupbox_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
更多推荐
发布评论