Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wpf 绑定到样式中属性的附加属性_Wpf_Xaml - Fatal编程技术网

Wpf 绑定到样式中属性的附加属性

Wpf 绑定到样式中属性的附加属性,wpf,xaml,Wpf,Xaml,在开始之前,我的最终目标是为所有控件提供默认的工具提示样式,当显示工具提示的控件出现验证错误时,该控件的正常边框将变为红色 因此,工具提示有一个PlacementTarget属性,它是显示工具提示的控件。当PlacementTarget控件发生数据绑定错误时,会在该控件上设置Validation.HasError附加属性。例如,我有一个文本框,在文本框样式中,我可以这样做: <Style TargetType="{x:Type TextBox}"> <Style.Tri

在开始之前,我的最终目标是为所有控件提供默认的工具提示样式,当显示工具提示的控件出现验证错误时,该控件的正常边框将变为红色

因此,工具提示有一个PlacementTarget属性,它是显示工具提示的控件。当PlacementTarget控件发生数据绑定错误时,会在该控件上设置Validation.HasError附加属性。例如,我有一个文本框,在文本框样式中,我可以这样做:

<Style TargetType="{x:Type TextBox}">
    <Style.Triggers>
        <Trigger Property="Validation.HasError" Value="True">
            <Setter Property="ToolTip">
                <Setter.Value>
                    <ToolTip Content="{Binding RelativeSource={RelativeSource Self}, Path=PlacementTarget.(Validation.Errors)[0].ErrorContent}"/>
我也试过这样的方法:

<Style TargetType="{x:Type ToolTip}">
    <Style.Triggers>
        <Trigger Property="PlacementTarget.Validation.HasError" Value="True">
            <Setter Property="BorderBrush" Value="Red"/>

但是我得到一个错误,说我不能像那样嵌套触发器的属性


有什么方法可以完成我在这里要做的吗?

搜索了几个小时的答案,发布后我看到了一篇相关的文章,得到了答案:您必须将Validation.hasrerror包装在括号中,它才能将其识别为附加属性

<Style TargetType="{x:Type ToolTip}">
    <Setter Property="BorderBrush" Value="{Binding Path=PlacementTarget.(Validation.HasError), RelativeSource={RelativeSource Self}, Converter={StaticResource ToolTipBorderConverter}}"/>

谢谢,同时找到了正确的答案!
<Style TargetType="{x:Type ToolTip}">
    <Style.Triggers>
        <Trigger Property="PlacementTarget.Validation.HasError" Value="True">
            <Setter Property="BorderBrush" Value="Red"/>
<Style TargetType="{x:Type ToolTip}">
    <Setter Property="BorderBrush" Value="{Binding Path=PlacementTarget.(Validation.HasError), RelativeSource={RelativeSource Self}, Converter={StaticResource ToolTipBorderConverter}}"/>