Janjko
Janjko

Reputation: 71

Updating data source on validation error

I have a TextBox that is bind to a property underneath. It has a two way binding with custom validation. If validation doesn't pass, and there's an error, the binding doesn't update the source. How can I make it so the source is updated, even though the validation has an error? I want validation to be more of a warning, not an error.

This is my simplified xaml:

<TextBox>
    <TextBox.Text>
       <Binding Path="Value"
        UpdateSourceTrigger="PropertyChanged">
          <Binding.ValidationRules>
             <validators:TelephoneNumberRule />
             <validators:NotOptionalRule />
          </Binding.ValidationRules>
       </Binding>
    </TextBox.Text>
</TextBox>

And the property:

public string Value { get; set; }

Thanks.

Upvotes: 0

Views: 873

Answers (1)

mm8
mm8

Reputation: 169200

Setting the ValidationStep property of a ValidationRule to UpdatedValue will cause it to be run after the source property has been updated:

<TextBox>
    <TextBox.Text>
        <Binding Path="Value" UpdateSourceTrigger="PropertyChanged">
            <Binding.ValidationRules>
                <validators:TelephoneNumberRule ValidationStep="UpdatedValue" />
                <validators:NotOptionalRule ValidationStep="UpdatedValue" />
            </Binding.ValidationRules>
        </Binding>
    </TextBox.Text>
</TextBox>

Upvotes: 1

Related Questions