A K P
A K P

Reputation: 91

Binding Error : Cannot find source for binding with reference 'RelativeSource FindAncestor

I have a customized Textbox which has some property SelfPropertyInfo(which further has some property like IsValid and RuleDescription).

I am trying to add below style on every Textbox of this type.

<Style TargetType="{x:Type CustomControls:TextBox}">
            <Setter Property="Height" Value="22"/>
            <Setter Property="Margin" Value="2,2,2,2"/>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="DarkGray" />
                </Trigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelfPropertyInfo.IsValid}" Value="False">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="ToolTip" >
                        <Setter.Value>
                            <ToolTip >                                
                                <TextBlock Foreground="Red" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomControls:TextBox},AncestorLevel=2},Path=SelfPropertyInfo.RuleDescription}"/>
                             </ToolTip>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
 </Style>

In above code I am not getting Tooltip Text. (Result of below code)

<TextBlock Foreground="Red" Text="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type CustomControls:TextBox},AncestorLevel=2},Path=SelfPropertyInfo.RuleDescription}"/>

I am getting below error:

System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='CustomControls.TextBox', AncestorLevel='2''. BindingExpression:Path=SelfPropertyInfo.RuleDescription; DataItem=null; target element is 'TextBlock' (Name=''); target property is 'Text' (type 'String')

Can anybody suggest the mistake I did in Text binding?

Note: I can't change the way Tooltip is added :(

Upvotes: 2

Views: 7643

Answers (1)

A K P
A K P

Reputation: 91

First of all I would like to say thanks to nkoniishvt for this link. It helped me to understand the problem. By using below code it worked as expected:

<Style TargetType="{x:Type CustomControls:TextBox}">
            <Setter Property="Height" Value="22"/>
            <Setter Property="Margin" Value="2,2,2,2"/>
            <Style.Triggers>
                <Trigger Property="IsEnabled" Value="False">
                    <Setter Property="Background" Value="DarkGray" />
                </Trigger>
                <DataTrigger Binding="{Binding RelativeSource={RelativeSource Self}, Path=SelfPropertyInfo.IsValid}" Value="False">
                    <Setter Property="BorderBrush" Value="Red"/>
                    <Setter Property="BorderThickness" Value="1"/>
                    <Setter Property="Tag" Value= "{Binding RelativeSource={RelativeSource Self}}"/>
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <ToolTip Height="28" Background="Red" DataContext="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}">
                                <TextBlock Foreground="White" Text="{Binding Path=SelfPropertyInfo.RuleDescription}"/>
                            </ToolTip>
                        </Setter.Value>
                    </Setter>
                </DataTrigger>
            </Style.Triggers>
  </Style>

Upvotes: 2

Related Questions