找到wpf中的所有子控件 [英] Find all the child controls in wpf

查看:38
本文介绍了找到wpf中的所有子控件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想找到 WPF 控件中的所有控件,特别是在 Datagrid、DataGridTemplateColumn 中.我查看了很多示例,似乎它们都需要将 Name 作为参数传递,或者根本不起作用.

I would like to find all of the controls within a WPF control specially in Datagrid, DataGridTemplateColumn. I have had a look at a lot of samples and it seems that they all either require a Name to be passed as parameter or simply do not work.

推荐答案

您尝试过哪些需要传递名称或根本不起作用"的情况?前者有什么问题?

What have you tried that "require a name to be passed or simply don't work" and what's wrong with the former?

private void FindAllChildren()
{
    var depObj = dataGrid;

    for (int i = 0; i < VisualTreeHelper.GetChildrenCount(depObj); i++)
    {
        DependencyObject child = VisualTreeHelper.GetChild(depObj, i);
        if (child is DataGridTemplateColumn)
        {
            // do a thing
        }
    }
}

改编自:https://stackoverflow.com/a/978352/1189566

这篇关于找到wpf中的所有子控件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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