如何在样式中绑定数据?

时间:2010-08-28 08:22:05

标签: xaml

  

此XAML标记不起作用:

<Label Content="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />

  

在此XAML代码中:

<Style x:Key="textBoxStyle" TargetType="{x:Type TextBox}">
            <Style.Triggers>
                <Trigger Property="Validation.HasError" Value="True">
                    <Setter Property="Background" Value="Red"/>
                    <!--<Setter Property="ToolTip" Value="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />-->
                    <Setter Property="ToolTip">
                        <Setter.Value>
                            <StackPanel Orientation="Vertical">
                                <Label Background="AliceBlue" Content="Input Error"/>
                                <Label Content="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>
  

我想在ToolTip属性中的Label中传递(Validation.Errors)[0]。ErrorContent数据。

1 个答案:

答案 0 :(得分:0)

RelativeSource Self上使用Label会将标签作为来源,而您需要TextBox。试试RelativeSource FindAncestor

<Label Content="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=(Validation.Errors)[0].ErrorContent}" />