Modifying a message (or exception) in WPF ValidatesOnException binding

I have a WPF application using MVVM. I use POCO object binding.

The text field is bound to a property in the object, for example:

           <TextBox.Text>
                <Binding Path="CertainProperty" Mode="TwoWay" >
                    <Binding.ValidationRules>
                        <ExceptionValidationRule/>
                    </Binding.ValidationRules>
                </Binding>
            </TextBox.Text>

Now this property is an int property, and when the user tries to enter a non-numeric value, they get "the input string is not in the correct format". What I need to do is configure this message to be more convenient.

How can i do this?

+3
source share
1 answer

, . , , , ValidationRule. :

public class Int32ValidationRule : ValidationRule
{
    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        if (string.IsNullOrEmpty((string)value))
            return ValidationResult.ValidResult;

        int number;
        return int.TryParse(value.ToString(), out number)
            ? ValidationResult.ValidResult
            : new ValidationResult(false, "Please enter a valid integer");
    }
}

, , CertainProperty string, IDataErrorInfo, .

0

Source: https://habr.com/ru/post/1712592/


All Articles