我有一个工具提示的样式和模板,并且想将工具提示中的一些内容绑定到父级的错误集合。我可以通过像下面的代码中那样显式设置 AncestorType 来使其工作,但我希望这可以全局应用。我试过使用 UIElement 和 FrameworkElement 没有运气,但我认为那是因为它没有一路向上找到正确的元素。
<ControlTemplate.Triggers>
<DataTrigger
Binding="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=(Validation.HasError)}"
Value="True">
<Setter
Property="Visibility"
TargetName="ErrorBorder"
Value="Visible" />
<Setter
Property="Text"
TargetName="ErrorText"
Value="{Binding RelativeSource={RelativeSource FindAncestor, AncestorType={x:Type TextBox}}, Path=(Validation.Errors)[0].ErrorContent}" />
</DataTrigger>
</ControlTemplate.Triggers>