Click here to Skip to main content
15,867,985 members
Articles / Desktop Programming / XAML

Finding a Child Control Inside Another Control

Rate me:
Please Sign up or sign in to vote.
3.80/5 (6 votes)
20 May 2009CPOL1 min read 49.8K   556   12   7
Finding a child control inside another control

Introduction

I had recently come across a scenario where I wanted to locate a child control inside another control. After some digging around, I was able to find a method that locates an ancestor, but was not able to find anything to locate a child.

So I decided to write some stuff of my own (based on the Find Ancestor code).

The Code

The problem with locating a child instance, knowing the type, is that we need to look through multiple children to find that type. Remember, any child can have more children and so on and so forth. So, obviously, recursion comes into the picture.

Again, another issue is, a set of children can contain multiple instances of the type we are looking for.

E.g. I am looking for a text block inside my grid below. Now which one would I return?

XML
<Grid>
<TextBlock x:Name="1"/>
<TextBlock x:Name="2"/>
</Grid>

Luckily, my scenario did not have such multiple instances. SO this is what I ran to find my child type.

C#
public static DependencyObject FindChild
	(DependencyObject dependencyObject, Func<DependencyObject, bool> predicate)
{
    if (searched != null) return searched;
    DependencyObject child = null;
    FrameworkElement frameworkElement = dependencyObject as FrameworkElement;
    if (frameworkElement != null)
    {
        int intCount = 
	System.Windows.Media.VisualTreeHelper.GetChildrenCount(frameworkElement);
        for (int i = 0; i < intCount; i++)
        {
            child = System.Windows.Media.VisualTreeHelper.GetChildrenCount
		(frameworkElement) == 0 ? null : 
		System.Windows.Media.VisualTreeHelper.GetChild(frameworkElement, i);
            if (predicate(child))
            {            
                searched = child; break;
            }
        FindChild(child, predicate);
        }
    }
    return searched;
}

This probably is resource intensive, as we are going to recursively look for a control instance among many children who could have many children and so on. But it worked for me!

You can download the source code for my sample. It's not the fanciest in the world, but it gives a fair idea of what I was trying to achieve.

One thing that we could do to get a specific instance of a control would be to use a FrameworkElement object's FindName method, but that would mean knowing the name of your control (from the XAML). This may not be possible at all times.

History

  • 20th May 2009: Article uploaded

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
India India

Comments and Discussions

 
QuestionMy Vote of 5 Pin
Amir Mahfoozi21-Dec-12 19:00
Amir Mahfoozi21-Dec-12 19:00 
Thanks Abhinav.

It helped me in my project Smile | :) .
Questioni know who are you, your are CHU Pin
Member 813814712-Aug-11 2:01
Member 813814712-Aug-11 2:01 
GeneralFrequency-shifting Auditory Feedback Pin
mehdiattar26-Apr-11 9:46
mehdiattar26-Apr-11 9:46 
GeneralRe: Frequency-shifting Auditory Feedback Pin
JP_Rocks26-Apr-11 23:35
JP_Rocks26-Apr-11 23:35 
GeneralMy vote of 1 Pin
Milton_win7-Jan-10 22:55
Milton_win7-Jan-10 22:55 
GeneralAlternate solution Pin
AndrusM29-May-09 10:36
AndrusM29-May-09 10:36 
GeneralRe: Alternate solution Pin
Abhinav S29-May-09 18:06
Abhinav S29-May-09 18:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.