Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7047299
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T02:45:16+00:00 2026-05-28T02:45:16+00:00

I have many labels as children of many different stack panels which are all

  • 0

I have many labels as children of many different stack panels which are all children of a list box, and I need to reference one of these labels were the Content.toString() == “criteria”. In other words, traversing the visual tree in WPF would be a ball ache because there are many parent/child methods to run. Is there a way of finding one of these labels on my window without it having a name and assuming I don’t know how far ‘down’ it is in the tree? Maybe there’s an item collection of everything in a window (without heirarchy) that I can run some LINQ against??

If you’re wondering why I don’t have a name for the labels – it’s because they are generated by a data template.

Thanks a lot,

Dan

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T02:45:16+00:00Added an answer on May 28, 2026 at 2:45 am

    I made a slight change to the code that @anatoliiG linked in order to return all the child controls of the specified type (instead of the first one):

    private IEnumerable<childItem> FindVisualChildren<childItem>(DependencyObject obj)
        where childItem : DependencyObject
    {
        for (int i = 0; i < VisualTreeHelper.GetChildrenCount(obj); i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(obj, i);
    
            if (child != null && child is childItem)
                yield return (childItem)child;
    
            foreach (var childOfChild in FindVisualChildren<childItem>(child))
                yield return childOfChild;
        }
    }
    

    With this function you could do something like this:

    var criteriaLabels =
        from cl in FindVisualChildren<Label>(myListBox)
        where cl.Content.ToString() == "criteria"
        select cl;
    
    foreach (var criteriaLabel in criteriaLabels)
    {
        // do stuff...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have all my controls laid out in interface builder (many labels, buttons etc).
My chart looks all jacked up. For one, there are too many labels along
I'm writing a WinForms application. I have many labels which represent some status (Enabled/Disabled
I have many buttons and labels on my c# form. I have a button
I have a simple viewcontroller with labels and text boxes. but to many to
I have many different NSArray 's stored in .dat files, in the Documents folder
I have many PowerPoint presentations that I need to be able to add to
I have a large panel with many child panels inside of it. Inside of
I have a view in my app with many labels and a toolbar with
i have a screen that contains one label field which contains lots of data

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.