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 :(
AncestorLevel
in there? – Mike Eason<Setter Property="Tag" Value="{Binding RelativeSource={RelativeSource Self},Path=SelfPropertyInfo.RuleDescription}"/>
2. Set DataContext of tooltip :<ToolTip DataContext="{Binding RelativeSource={RelativeSource Self},Path=PlacementTarget.Tag}">
3. Changed the TextBlock's text:<TextBlock Foreground="Red" Text="{Binding RelativeSource={RelativeSource Self},Path=SelfPropertyInfo.RuleDescription}"/>
– A K P