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 6794585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T18:14:22+00:00 2026-05-26T18:14:22+00:00

Within my Windows.Resources I have the following column defined: <DataGridTextColumn x:Key=CustomColumn x:Shared=False> <DataGridTextColumn.Header> <StackPanel>

  • 0

Within my Windows.Resources I have the following column defined:

    <DataGridTextColumn x:Key="CustomColumn" x:Shared="False">
        <DataGridTextColumn.Header>
            <StackPanel>
                <Label Padding="0" Name="labelA"/>
                <Separator HorizontalAlignment="Stretch"/>
                <Label Padding="0" Name="labelB"/>
            </StackPanel>
        </DataGridTextColumn.Header>
    </DataGridTextColumn>

I have an event that gets fired from my ViewModel and adds the following “CustomColumn” to my DataGrid:

            var column = FindResource("CustomColumn") as DataGridTextColumn;
            var label = FindName("labelA") as Label;
            label.Content = string.Format("A {0}", i);
            DataGrid1.Columns.Add(column); 

The question is, how would I change the content of the two labels inside the CustomColumn header? I above code fails because it is unable to find “labelA”. (adding the column works, but I also need to set these labels). My guess is, I need to find it through the VisualTree — but I want to make sure I’m not doing anything else wrong.

Thanks for the help.

  • 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-26T18:14:23+00:00Added an answer on May 26, 2026 at 6:14 pm

    I created some Visual Tree helpers that I use all the time to find objects in the Visual Tree

    For example, you can find a Label named “LabelA” with this:

    VisualTreeHelpers.FindChild<Label>(column, "LabelA");
    

    Here’s the FindChild method in case the above link doesn’t work

    using System.Windows;
    using System.Windows.Media;
    
    namespace MyNamespace
    {
        public class VisualTreeHelpers
        {
            /// <summary>
            /// Looks for a child control within a parent by name
            /// </summary>
            public static T FindChild<T>(DependencyObject parent, string childName)
            where T : DependencyObject
            {
                // Confirm parent and childName are valid.
                if (parent == null) return null;
    
                T foundChild = null;
    
                int childrenCount = VisualTreeHelper.GetChildrenCount(parent);
                for (int i = 0; i < childrenCount; i++)
                {
                    var child = VisualTreeHelper.GetChild(parent, i);
                    // If the child is not of the request child type child
                    T childType = child as T;
                    if (childType == null)
                    {
                        // recursively drill down the tree
                        foundChild = FindChild<T>(child, childName);
    
                        // If the child is found, break so we do not overwrite the found child.
                        if (foundChild != null) break;
                    }
                    else if (!string.IsNullOrEmpty(childName))
                    {
                        var frameworkElement = child as FrameworkElement;
                        // If the child's name is set for search
                        if (frameworkElement != null &amp;&amp; frameworkElement.Name == childName)
                        {
                            // if the child's name is of the request name
                            foundChild = (T)child;
                            break;
                        }
                        else
                        {
                            // recursively drill down the tree
                            foundChild = FindChild<T>(child, childName);
    
                            // If the child is found, break so we do not overwrite the found child.
                            if (foundChild != null) break;
                        }
                    }
                    else
                    {
                        // child element found.
                        foundChild = (T)child;
                        break;
                    }
                }
    
                return foundChild;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to embed a windows form within another windows form? I have
In my MainWindow.xaml, I have the following reference to a ResourceDictionary: <Window.Resources> <ResourceDictionary> <ResourceDictionary.MergedDictionaries>
Lets say I have this bit of code: <Window> <Window.Resources> <Color x:Key=MyColor A=255 R=152
I have a Windows service written in Delphi. One of the third-party resources it
I want to create applications in windows that has complete portability (within windows OSes
I'm looking for a program to create window transparency within Windows XP; somewhat like
I want to pop a browser with a given url from within a windows
Greetings! I am trying to check directory write-permissions from within a Windows MFC/ATL program
It's possible, but is it appropriate to use SHFileOperation within a Windows service? All
I am using windows authentication within an ASP.NET application. I am wondering how to

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.