Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable validation on disabled TextBox

Tags:

wpf

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

like image 763
apocalypse Avatar asked Aug 18 '14 00:08

apocalypse


1 Answers

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>
like image 140
Yusuf Tarık Günaydın Avatar answered Sep 19 '22 00:09

Yusuf Tarık Günaydın