apocalypse
apocalypse

Reputation: 5884

Disable validation on disabled TextBox

<Binding Path="AttachmentName" UpdateSourceTrigger="PropertyChanged">
                          <Binding.ValidationRules>
                                <valid:AttachmentNameValidationRule ValidationStep="RawProposedValue" ValidatesOnTargetUpdated="True"/>
                          </Binding.ValidationRules>
                    </Binding>

Validator checks if entered value is in proper format: file.extension. Unfortunelly, textBox is red when control IsEnabled == false (because content is "").

Any idea how to disable validation? Tried some stuff with x:referense and passing UIElement to validator, but didnt work.

Upvotes: 8

Views: 6148

Answers (1)

The original credit goes to the link in @ydoow's answer.

It is easily achieved with a trigger that sets the Validation.ErrorTemplate to null when the control is disabled.

The property is still validated but user is not notified about validation results.

<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>

Upvotes: 9

Related Questions