Click here to Skip to main content
14,087,117 members
Click here to Skip to main content
Add your own
alternative version

Tagged as


12 bookmarked
Posted 20 May 2009
Licenced CPOL

Finding a Child Control Inside Another Control

, 20 May 2009
Rate this:
Please Sign up or sign in to vote.
Finding a child control inside another control


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?

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

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

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 = 
        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.


  • 20th May 2009: Article uploaded


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


About the Author

Abhinav S
Software Developer (Senior)
India India

You may also be interested in...


Comments and Discussions

QuestionMy Vote of 5 Pin
Amir Mahfoozi21-Dec-12 19:00
memberAmir Mahfoozi21-Dec-12 19:00 
Questioni know who are you, your are CHU Pin
Member 813814712-Aug-11 2:01
memberMember 813814712-Aug-11 2:01 
GeneralFrequency-shifting Auditory Feedback Pin
mehdiattar26-Apr-11 9:46
membermehdiattar26-Apr-11 9:46 
GeneralRe: Frequency-shifting Auditory Feedback Pin
JP_Rocks26-Apr-11 23:35
memberJP_Rocks26-Apr-11 23:35 
GeneralMy vote of 1 Pin
Milton_win7-Jan-10 22:55
memberMilton_win7-Jan-10 22:55 
GeneralAlternate solution Pin
AndrusM29-May-09 10:36
memberAndrusM29-May-09 10:36 
GeneralRe: Alternate solution Pin
Abhinav S29-May-09 18:06
memberAbhinav 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.

Permalink | Advertise | Privacy | Cookies | Terms of Use | Mobile
Web02 | 2.8.190518.1 | Last Updated 20 May 2009
Article Copyright 2009 by Abhinav S
Everything else Copyright © CodeProject, 1999-2019
Layout: fixed | fluid