Cannot find source for binding with reference 'RelativeSource FindAncestor'

36,955

DataGridTemplateColumn is not part of the visual or logical tree, and therefore has no binding ancestor (or any ancestor) so the RelativeSource doesn't work.

Instead you have to give the binding the source explicitly.

<UserControl.Resources>
    <local:BindingProxy x:Key="proxy" Data="{Binding}" />
</UserControl.Resources>

<DataGridTemplateColumn Visibility="{Binding Data.IsVisible, 
    Source={StaticResource proxy},
    Converter={StaticResource BooleanToVisibilityConverter}}">

And the binding proxy.

public class BindingProxy : Freezable
{
    protected override Freezable CreateInstanceCore()
    {
        return new BindingProxy();
    }
 
    public object Data
    {
        get { return (object)GetValue(DataProperty); }
        set { SetValue(DataProperty, value); }
    }
 
    // Using a DependencyProperty as the backing store for Data.
    // This enables animation, styling, binding, etc...
    public static readonly DependencyProperty DataProperty =
        DependencyProperty.Register("Data", typeof(object), 
        typeof(BindingProxy), new UIPropertyMetadata(null));
}

Credits.

Share:
36,955
Hodaya Shalom
Author by

Hodaya Shalom

Updated on August 06, 2022

Comments

  • Hodaya Shalom
    Hodaya Shalom almost 2 years

    I get this error:

    Cannot find source for binding with reference 'RelativeSource FindAncestor,
    AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1''
    

    On this Binding:

    <DataGridTemplateColumn Visibility="{Binding DataContext.IsVisible,
         RelativeSource={RelativeSource AncestorType={x:Type UserControl}},
         Converter={StaticResource BooleanToVisibilityConverter}}">
    

    The ViewModel is sitting as DataContext in UserControl. The DataContext of the DataGrid (sitting in UserControl) is property within the ViewModel, in ViewModel I have a variable that says whether to show a certain line or not, its binding fails, why?

    Here my property :

    private bool _isVisible=false;
    public bool IsVisible
    {
        get { return _isVisible; }
        set
        {
            _isVisible= value;
            NotifyPropertyChanged("IsVisible");
        }
    }
    

    When it comes to the function: NotifyPropertyChanged the PropertyChanged event null - mean he failed to register for the binding.

    It should be noted that I have more bindings to ViewModel in such a way that works, here is an example:

    Command="{Binding DataContext.Cmd,
    RelativeSource={RelativeSource AncestorType={x:Type UserControl}}}" 
    
  • Hodaya Shalom
    Hodaya Shalom over 11 years
    Now i get this error: BindingExpression path error: 'IsVisible' property not found on 'object' ''BindingProxy'
  • Cameron MacFarland
    Cameron MacFarland over 11 years
    Whoops, that should be Data.IsVisible.
  • Hodaya Shalom
    Hodaya Shalom over 11 years
    Great! It works at last, thank you very much.
  • Admin
    Admin over 11 years
    You should be able to use an ElementName="whatevs" and avoid this redirection.
  • Hodaya Shalom
    Hodaya Shalom over 11 years
    @Will, I tried this way - it did not work.
  • Cameron MacFarland
    Cameron MacFarland over 11 years
    @Will ElementName uses the logical tree. When the element doesn't use the logical tree you're SOL. stackoverflow.com/questions/705853/…
  • Informagic
    Informagic over 5 years
    Outstanding. It’s also the solution in cases where the ContextMenu is defined within a custom TriggerAction—see stackoverflow.com/questions/54415381/….