另一种解决方案是IsEnabled
通过AdornedElement
. AdornedElementPlaceholder
在下面的示例中,我使用IsEnabled="{Binding ElementName=customAdorner, Path=AdornedElement.IsEnabled}"
. 然后你就可以正常触发了IsEnabled
。
<Style x:Key="TextBoxValidationStyle" TargetType="{x:Type TextBox}">
<Setter Property="Validation.ErrorTemplate">
<Setter.Value>
<ControlTemplate>
<DockPanel IsEnabled="{Binding ElementName=customAdorner, Path=AdornedElement.IsEnabled}">
<AdornedElementPlaceholder x:Name="customAdorner">
<Border x:Name="Border" BorderThickness="1">
<Border.Style>
<Style TargetType="Border">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="BorderBrush" Value="Transparent" />
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="BorderBrush" Value="Red" />
</Trigger>
</Style.Triggers>
</Style>
</Border.Style>
</Border>
</AdornedElementPlaceholder>
</DockPanel>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
好处是,如果您的ErrorTemplate
.