wpf 绑定错误:无法找到引用“RelativeSource FindAncestor”的绑定源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34591499/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 13:36:36  来源:igfitidea点击:

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

wpfxaml

提问by A K P

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

我有一个自定义的文本框,它有一些属性 SelfPropertyInfo(它还有一些像 IsValid 和 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')

System.Windows.Data 错误:4:无法找到引用“RelativeSource FindAncestor、AncestorType='CustomControls.TextBox'、AncestorLevel='2'”的绑定源。BindingExpression:Path=SelfPropertyInfo.RuleDescription;数据项=空;目标元素是 'TextBlock' (Name=''); 目标属性是“文本”(类型“字符串”)

Can anybody suggest the mistake I did in Text binding?

有人可以建议我在文本绑定中犯的错误吗?

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

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

回答by A K P

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

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

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