开发者

How can I bind a data inside a style?

开发者 https://www.devze.com 2023-01-13 05:27 出处:网络
This XAML tag doesn\'t work: <Label Content=\"{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}\" />

This XAML tag doesn't work:

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

in this XAML code:

<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 Conten开发者_如何学Ct="{Binding RelativeSource={RelativeSource Self}, Path=(Validation.Errors)[0].ErrorContent}" />
                            </StackPanel>
                        </Setter.Value>
                    </Setter>
                </Trigger>
            </Style.Triggers>
        </Style>

I want to pass (Validation.Errors)[0].ErrorContent data of a TextBox in a Label inside ToolTip property


Using RelativeSource Self on a Label will get you the Label as the source, whereas you want the TextBox. Try a RelativeSource FindAncestor:

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

精彩评论

暂无评论...
验证码 换一张
取 消