绑定错误:找不到与引用' RelativeSource FindAncestor绑定的源

时间:2016-01-04 12:44:39

标签: wpf xaml

我有一个自定义的Textbox,它有一些属性SelfPropertyInfo(还有一些属性,如IsValid和RuleDescription)。

我正在尝试在此类型的每个文本框上添加以下样式。

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

在上面的代码中,我没有得到Tooltip Text。 (以下代码的结果)

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

我收到以下错误:

  

System.Windows.Data错误:4:找不到绑定源   参考&#39; RelativeSource FindAncestor,   AncestorType =&#39; CustomControls.TextBox&#39;,AncestorLevel =&#39; 2&#39;&#39;。   BindingExpression:路径= SelfPropertyInfo.RuleDescription;   的DataItem = NULL;目标元素是&#39; TextBlock&#39; (名称=&#39;&#39);目标   财产是&#39; Text&#39; (键入&#39; String&#39;)

有人能说出我在文字绑定中犯的错误吗?

注意:我无法改变添加工具提示的方式:(

1 个答案:

答案 0 :(得分:2)

首先,我要对nkoniishvt感谢link。它帮助我理解了这个问题。通过使用下面的代码,它按预期工作:

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