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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T14:34:56+00:00 2026-05-25T14:34:56+00:00

I am writing some code which programmatically creates bindings on the fly, but I

  • 0

I am writing some code which programmatically creates bindings on the fly, but I can’t seem to read the value resulting from a binding whose RelativeSourceMode is set to FindAncestor. I was wondering if anybody has successfully created a RelativeSource binding in code (not XAML) with this mode?

With Binding tracing turned the warning is:

System.Windows.Data Warning: 64 : BindingExpression (hash=57957548): RelativeSource (FindAncestor) requires tree context

Here is sample code which creates the RelativeSource binding:

 private void ButtonBase_OnClick(object sender, RoutedEventArgs e)
 {
        // Create RelativeSource FindAncestor Binding
        var binding = new Binding
                             {
                                 RelativeSource = new RelativeSource(RelativeSourceMode.FindAncestor, typeof(ListBoxItem), 1),
                                 Path = new PropertyPath("Tag"),
                             };

        PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);
        BindingOperations.SetBinding(textBlock, TagProperty, binding);

        // Always null
        var findAncestorBindingResult = textBlock.Tag;

        // Create RelativeSource Self Binding
        binding = new Binding
        {
            RelativeSource = new RelativeSource(RelativeSourceMode.Self),
            Path = new PropertyPath("Text"),
        };

        PresentationTraceSources.SetTraceLevel(binding, PresentationTraceLevel.High);
        BindingOperations.SetBinding(textBlock, TagProperty, binding);

        // Has correct value Text property set from XAML
        var selfBindingResult = textBlock.Tag;
    }

And here is the corresponding XAML:

    <StackPanel>
        <ListBox x:Name="listBox">
            <ListBoxItem x:Name="listBoxItem" Tag="Item One" >
                <ListBoxItem.Content>
                    <TextBlock x:Name="textBlock">
                        <TextBlock.Text>
                            <Binding RelativeSource="{RelativeSource Mode=FindAncestor, AncestorType={x:Type ListBoxItem}}" Path="Tag" />
                        </TextBlock.Text>
                    </TextBlock>
                </ListBoxItem.Content>
            </ListBoxItem>
        </ListBox>
        <Button Content="Debug" Click="ButtonBase_OnClick" />
    </StackPanel>

The tree is loaded, so I could simulate the FindAncestor binding (using VisualTreeHelper.GetParent(...) to locate the target element of the FindAncestor binding and then just apply a RelativeSource Self binding to it) but I’m curious as to why this doesn’t work.

Thanks in advance!

  • 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-25T14:34:57+00:00Added an answer on May 25, 2026 at 2:34 pm

    You cannot get the value of a bound property right after binding it, you are currently blocking the UI-thread with your handler operation, the binding will only take place after that when the thread idles (i think).

    You should remove everything after the Always null comment and check the value later, e.g. in the handler of another button. Also, is the bound element actually in the tree as shown in XAML just without the binding? If not that would also explain such an error.

    Edit: I just noticed that your bindings might be a bit off, they do not translate to the XAML you posted as in the XAML you bind the Text and in your code you set the binding on the TagProperty. Ignoring that the binding should work in theory, just note that immediately after setting the bindig the value of the bound property will be null as mentioned earlier, so do not remove it right away (and bind the TextProperty if you want visual results).

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

Sidebar

Related Questions

I am writing some code in which i need to get a line from
I'm writing code to download email from various servers, some of which are outside
I'm writing some native C++ code which needs to be called from C# (and
I'm currently writing some code which will download images from my web server for
I'm writing some code which could really do with some simple compile time metaprogramming.
I'm writing some code for a class constructor which loops through all the properties
I'm writing a JUnit test for some code that produces an Excel file (which
I am writing some C code which is expected to compile on multiple compilers
I'm writing some JDBC code which calls a Oracle 11g PL/SQL procdedure which has
I'm writing some JDBC code which calls a Oracle 11g PL/SQL procdedure which has

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.