<Binding Path="AttachmentName" UpdateSourceTrigger="PropertyChanged">
<Binding.ValidationRules>
<valid:AttachmentNameValidationRule ValidationStep="RawProposedValue" ValidatesOnTargetUpdated="True"/>
</Binding.ValidationRules>
</Binding>
Run Code Online (Sandbox Code Playgroud)
验证器检查输入的值是否采用正确的格式:file.extension。不幸的是,当控件 IsEnabled == false 时 textBox 是红色的(因为内容是“”)。
知道如何禁用验证吗?用 x:referense 尝试了一些东西并将 UIElement 传递给验证器,但没有用。
原始功劳转到@ydoow 答案中的链接。
使用在Validation.ErrorTemplate禁用控件时将设置为 null的触发器可以轻松实现。
该属性仍然经过验证,但不会通知用户有关验证结果的信息。
<Style TargetType="TextBox" BasedOn="{StaticResource {x:Type TextBox}}">
<Style.Triggers>
<Trigger Property="IsEnabled" Value="False">
<Setter Property="Validation.ErrorTemplate" Value="{x:Null}"/>
</Trigger>
<Trigger Property="IsEnabled" Value="True">
<Setter Property="Validation.ErrorTemplate" Value="{StaticResource ErrorTemplate}"/>
</Trigger>
</Style.Triggers>
</Style>
Run Code Online (Sandbox Code Playgroud)