简体   繁体   中英

Find all the child controls in wpf

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
        }
    }
}

Adapted from here: https://stackoverflow.com/a/978352/1189566

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM