简体   繁体   English

绑定错误:找不到参考'RelativeSource FindAncestor的绑定源

[英]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). 我有一个自定义的文本框,它具有一些属性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''. System.Windows.Data错误:4:找不到参考'RelativeSource FindAncestor,AncestorType ='CustomControls.TextBox',AncestorLevel ='2''的绑定源。 BindingExpression:Path=SelfPropertyInfo.RuleDescription; BindingExpression:路径= SelfPropertyInfo.RuleDescription; DataItem=null; 的DataItem = NULL; target element is 'TextBlock' (Name=''); 目标元素是'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 :( 注意:我无法更改添加工具提示的方式:(

First of all I would like to say thanks to nkoniishvt for this link . 首先,我要感谢nkoniishvt的此链接 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>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法找到使用引用“RelativeSource FindAncestor”的绑定源 - Cannot find source for binding with reference 'RelativeSource FindAncestor WPF DataBinding错误:找不到参考“ RelativeSource FindAncestor”进行绑定的源 - WPF DataBinding Error: Cannot find source for binding with reference 'RelativeSource FindAncestor' SurfaceListBox isSelected-System.Windows.Data错误:4:找不到参考&#39;RelativeSource FindAncestor的绑定源 - SurfaceListBox isSelected - System.Windows.Data Error: 4 : Cannot find source for binding with reference 'RelativeSource FindAncestor 防止WPF绑定异常:找不到与引用&#39;RelativeSource FindAncestor ...绑定的源代码来降低性能 - Prevent WPF Binding Exception: Cannot find source for binding with reference 'RelativeSource FindAncestor…' reduces performance 找不到参考&#39;RelativeSource FindAncestor,AncestorType =&#39;System.Windows.Controls.UserControl&#39;,AncestorLevel =&#39;1&#39;&#39;的绑定源 - Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='System.Windows.Controls.UserControl', AncestorLevel='1'' 无法找到引用绑定的源'RelativeSource FindAncestor,AncestorType ='MahApps.Metro.Controls.Glow',AncestorLevel ='1'' - Cannot find source for binding with reference 'RelativeSource FindAncestor, AncestorType='MahApps.Metro.Controls.Glow', AncestorLevel='1'' 以编程方式创建RelativeSource FindAncestor绑定 - Programmatically creating a RelativeSource FindAncestor binding WPF绑定无法找到与引用绑定的源 - WPF Binding Cannot find source for binding with reference 无法找到带参考的绑定源 - Cannot find source for binding with reference WPF 数据绑定的“RelativeSource FindAncestor”究竟是做什么的? - What exactly does WPF Data Binding's “RelativeSource FindAncestor” do?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM