如何在 WPF 中的组框中获取控件列表 [英] How to get a list of controls in a groupbox in WPF

查看:36
本文介绍了如何在 WPF 中的组框中获取控件列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在标准的 WinForms 开发中,我会执行以下操作:

In standard WinForms development I would do the following:

foreach (Control in groupBox1.Controls)
{
     MessageBox.Show(c.Name);
}

一个人如何在 WPF 中做到这一点?我在 GroupBox 中有一个 Grid,在网格中有许多控件(按钮等),但似乎无法弄清楚如何获取每个控件.

How does a guy do this in WPF? I have a Grid inside the GroupBox and a number of controls in the grid (buttons etc.) but can't seem to figure out how to get each control.

推荐答案

正如 MSDN 所建议的,您需要将控件迭代为 GroupBox 的子项.另外,请注意,您通常需要将 Grid 添加到 GroupBox 中,以便能够将新控件添加到该 GroupBox 中.因此,您需要在该 GroupBox 中获取 Grid 的子项并遍历它们,如下所示:

As MSDN advises, you will need to iterate the controls as children of the GroupBox. Also, note that you usually need to add a Grid into your GroupBox to be able to add new controls into that GroupBox. So you will need to get the children of the Grid in that GroupBox and iterate through them, something like this:

//iterate through the child controls of "grid"
int count = VisualTreeHelper.GetChildrenCount(grid);
            for (int i = 0; i < count; i++)
            {
              Visual childVisual = (Visual)VisualTreeHelper.GetChild(grid, i);
                if (childVisual is TextBox)
                {
                    //write some logic code
                }
               else
               {

               }
            }

您可能会发现这很有用:http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93ebca8f-2977-4c36-b437-9c82a22266f6

You might find this useful: http://social.msdn.microsoft.com/Forums/en-US/wpf/thread/93ebca8f-2977-4c36-b437-9c82a22266f6

这篇关于如何在 WPF 中的组框中获取控件列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆