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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T21:23:30+00:00 2026-05-17T21:23:30+00:00

How can I get access to an element (TextBlock) within the DataTemplate of GridViewColumnHeader

  • 0

How can I get access to an element (TextBlock) within the DataTemplate of GridViewColumnHeader from the code????
I want to set focus on the column header.

  • 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-17T21:23:31+00:00Added an answer on May 17, 2026 at 9:23 pm

    Just any GridViewColumnHeader or one in particular? You can use this code

    List<GridViewColumnHeader> allGridViewColumnHeaders = GetVisualChildCollection<GridViewColumnHeader>(listView);
    foreach (GridViewColumnHeader columnHeader in allGridViewColumnHeaders)
    {
        TextBlock textBlock = GetVisualChild<TextBlock>(columnHeader);
        if (textBlock != null)
        {
    
        }
    }
    

    And the helper methods

    public List<T> GetVisualChildCollection<T>(object parent) where T : Visual
    {
        List<T> visualCollection = new List<T>();
        GetVisualChildCollection(parent as DependencyObject, visualCollection);
        return visualCollection;
    }
    
    private void GetVisualChildCollection<T>(DependencyObject parent, List<T> visualCollection) where T : Visual
    {
        int count = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < count; i++)
        {
            DependencyObject child = VisualTreeHelper.GetChild(parent, i);
            if (child is T)
            {
                visualCollection.Add(child as T);
            }
            else if (child != null)
            {
                GetVisualChildCollection(child, visualCollection);
            }
        }
    }
    
    private T GetVisualChild<T>(DependencyObject parent) where T : Visual
    {
        T child = default(T);
        int numVisuals = VisualTreeHelper.GetChildrenCount(parent);
        for (int i = 0; i < numVisuals; i++)
        {
            Visual v = (Visual)VisualTreeHelper.GetChild(parent, i);
            child = v as T;
            if (child == null)
            {
                child = GetVisualChild<T>(v);
            }
            if (child != null)
            {
                break;
            }
        }
        return child;
    }
    

    Update

    To make a GridViewColumnHeader get focus you can

    columnHeader.Focus();
    

    Depending on where you do this it might not work, then you can try

    EventHandler eventHandler = null;
    eventHandler = new EventHandler(delegate
    {
        listView.LayoutUpdated -= eventHandler;
        GridViewColumnHeader columnHeader = GetVisualChild<GridViewColumnHeader>(listView);
        columnHeader.Focus();
    });
    listView.LayoutUpdated += eventHandler;
    

    Also make sure that your GridViewColumnHeader has the following attributes

    <GridViewColumnHeader IsTabStop="True" Focusable="True">
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to access and element from within a frameset frame. For example if
i want to know if i can access the element that triggers a function
I want to set a variable from a flash file so that I can
If all tables I want to delete from have the column gamer_id can i
How can I access this element: <input type=submit value=Save as XML onclick=some code goes
In widget's code I need to get access to HTML id of the rendered
I have a via with some jQuery function to get value from HTML element
I want to access the 'url' attribute in the media:content element below. I am
Can i get the source code for a WAMP stack installer somewhere? Any help
I would like to get a sum from a column, with and without a

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.