Programmatically creating a RelativeSource FindAncestor binding

FunnyItWorkedLastTime picture FunnyItWorkedLastTime · Sep 16, 2011 · Viewed 7.5k times · Source

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!

Answer

H.B. picture H.B. · Sep 16, 2011

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).