【发布时间】:2013-10-17 11:37:29
【问题描述】:
我有一个工具提示的样式和模板,并希望将工具提示中的一些内容绑定到父级的错误集合。我可以通过像下面的代码中那样显式设置 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>
【问题讨论】:
-
所以你想让这个赋值在逻辑树上冒泡,直到找到一个可以接受绑定的元素,然后再赋值?我不知道你将能够在 Xaml 中执行此操作。
标签: wpf data-binding controltemplate