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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T02:14:30+00:00 2026-05-21T02:14:30+00:00

How can I get the Binding Path of an Element in a DataTemplate? My

  • 0

How can I get the Binding Path of an Element in a DataTemplate?
My XAML looks like this:

<GridViewColumn Header="Double">
    <GridViewColumn.CellTemplate>
        <DataTemplate>
            <TextBlock Text="{Binding TotalValues, Mode=OneWay, StringFormat=\{0:0\'0.00\}, Converter={StaticResource GridValueConverter}}" TextAlignment="Right" Width="auto"/>
        </DataTemplate>
    </GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Header="Comments" DisplayMemberBinding="{Binding Path=Comments, Mode=OneWay}" Width="auto"/>

To get the Binding Path for the “normal” GridViewColumnHeader.DisplayMemberBinding is

var field = (string)((Binding)((GridViewColumnHeader)e.OriginalSource).Column.DisplayMemberBinding).Path.Path;

How can I get the same for the Binding Path of TextBlock.Text?

  • 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-21T02:14:30+00:00Added an answer on May 21, 2026 at 2:14 am

    This is a great question. There’s a separation between the code and the XAML, and, code-wise, it’s not immediately obvious where to start looking. Also, the DataTemplate is compiled into BAML so it’s not very accessible at runtime.

    There are at least two strategies for finding the binding’s path.

    The first strategy is saving the path as a static variable somewhere.

    Code-behind:

    namespace TempProj
    {
        using System.Windows;
    
        public partial class MainWindow : Window
        {
            static public readonly PropertyPath BindingPath = new PropertyPath("X");
    
            public MainWindow()
            {
                InitializeComponent();
            }
        }
    }
    

    XAML:

    <Window x:Class="TempProj.MainWindow"
            xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
            xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
            xmlns:local="clr-namespace:TempProj"
            Title="MainWindow" Height="350" Width="525">
        <Window.Resources>
            <Vector3DCollection x:Key="Coordinates">
                <Vector3D X="1" Y="0" Z="0"/>
                <Vector3D X="0" Y="22" Z="0"/>
                <Vector3D X="0" Y="0" Z="333"/>
                <Vector3D X="0" Y="4444" Z="0"/>
                <Vector3D X="55555" Y="0" Z="0"/>
            </Vector3DCollection>
        </Window.Resources>
        <ListView x:Name="lv" ItemsSource="{StaticResource Coordinates}">
            <ListView.View>
                <GridView>
                    <GridViewColumn Header="{x:Static local:MainWindow.BindingPath}">
                        <GridViewColumn.CellTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Path={x:Static local:MainWindow.BindingPath}}"/>
                            </DataTemplate>
                        </GridViewColumn.CellTemplate>
                    </GridViewColumn>
                </GridView>
            </ListView.View>
        </ListView>
    </Window>
    

    The second strategy is opening Snoop or WPF Inspector. The goal is to programmatically search the visual tree for the TextBlock of interest. However, there could many TextBlocks in the ListView. In fact, the Header is probably using one. So, the first step is to identify a unique ancestor of the cell’s TextBlock. Looking at the visual tree, there are two decent candidates: a ScrollContentPresenter (with a template part name, which should be unique) and a GridViewRowPresenter. It’s best for the ancestor to be close to the TextBlock of interest. This decreases the likelihood of other TextBlocks distorting the search results. Thus, the GridViewRowPresenter is preferable.

    enter image description here

    One or two utility methods are added to perform the visual tree search.

    static public class ControlAux
    {
        static public IEnumerable<T> GetVisualDescendants<T>(this DependencyObject item) where T : DependencyObject
        {
            for (int i = 0; i < VisualTreeHelper.GetChildrenCount(item); ++i)
            {
                DependencyObject child = VisualTreeHelper.GetChild(item, i);
                if (typeof(T) == (child.GetType()))
                {
                    yield return (T)child;
                }
                foreach (T descendant in GetVisualDescendants<T>(child))
                {
                    yield return descendant;
                }
            }
        }
        static public T FindVisualDescendant<T>(this DependencyObject item, string descendantName) where T : DependencyObject
        {
            return
                GetVisualDescendants<T>(item).Where(
                descendant =>
                {
                    var frameworkElement = descendant as FrameworkElement;
                    return frameworkElement != null ? frameworkElement.Name == descendantName : false;
                }).
                FirstOrDefault();
        }
    }
    

    Now, two searches through the visual tree are performed, with the first search result acting as the root for the second search. Starting with the ListView, a GridViewRowPresenter is found. Starting with that GridViewRowPresenter, a TextBlock is found. Its Text binding is queried and the path is finally accessed.

    GridViewRowPresenter gvrp = lv.GetVisualDescendants<GridViewRowPresenter>().FirstOrDefault();
    TextBlock tb = gvrp.GetVisualDescendants<TextBlock>().FirstOrDefault();
    string path = BindingOperations.GetBinding(tb, TextBlock.TextProperty).Path.Path;
    

    It’s important to note that the ListView’s ControlTemplates and DataTemplates must be inflated into their actual visual elements for the search to work. If the inflation hasn’t happened, the elements don’t exist. You can test this by first trying the search in the main window’s contructor and then trying it in the window’s OnSourceInitialized. Also, all error checking has been left out for brevity.

    Finally, this second strategy is not even remotely bulletproof. WPF elements can have arbitrarily complex visual compositions when new ControlTemplates and DataTemplates are used. However, it’s a good starting point for thinking about how you might solve the problem in whatever situation you’re in.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i can't get this binding working in XAML. Binding in c# works: public partial
My build path in Eclipse looks like this: ProjectName -- WEB-INF -- classes --
This probably sounds really stupid, but I just can't get a binding to an
After binding the scroll event to an object, how can I get the amount
Why can't I do a simple: Get-Website | where { $_.Bindings -like *DOMAIN* }
I can get the file path with: (message (file-name-directory (or buffer-file-name load-file-name))) But if
I can get this to work: [<DllImport(user32.dll)>] extern bool GetClientRect(nativeint, RECT*) let getClientRect hwnd
I'm fairly new to Data binding & XAML, so this probably is fairly simple
I try to use binding with an attached property. But can't get it working.
Are there Python 3 bindings for Clutter? If so, how can I get them

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.