Joe
Joe

Reputation: 7004

Custom validation: returning ValidationResult(true, null) still fails validation

I'm entering a value that passes my custom validation check, yet WPF still comes up with a fail message:

enter image description here

Validation code parses the percentage, converts to a double. Using the debugger, I can step through and everything works fine. For a value of "50 %" it parses 0.5, steps through and gets to the last line return new ValidationResult(true, null);

However, it still causes a failed validation, with a default message "Value '50 %' could not be converted.".

    public override ValidationResult Validate(object value, CultureInfo cultureInfo)
    {
        double valueToValidate = 0;

        try
        {
            string trimmed = (value as string).TrimEnd(new char[] { '%', ' ' });
            valueToValidate = Double.Parse(trimmed) / 100;
        }
        catch (Exception e)
        {
            return new ValidationResult(false, "Illegal characters or " + e.Message);
        }

        if ((valueToValidate < Min) || (valueToValidate > Max))
        {
            return new ValidationResult(false,
              "Please enter a value in the range: " + Min + " - " + Max + ".");
        }
        else
        {
            return new ValidationResult(true, null);
        }
    }

xaml:

                        <TextBox Grid.Column="2" Width="45">
                            <TextBox.Text>
                                <Binding Path="AppController.Zoom" StringFormat="{}{0:P0}">
                                    <Binding.ValidationRules>
                                        <helpers:MinMaxPercentageValidationRule Min="0.4" Max="2"/>
                                    </Binding.ValidationRules>
                                </Binding>
                            </TextBox.Text>
                        </TextBox>

The failure states work fine, prompting with the appropriate message. It just seems to be the

Upvotes: 1

Views: 3184

Answers (1)

nicolascolman
nicolascolman

Reputation: 579

In .NET Framework:

return ValidationResult.ValidResult;

In .NET Core:

return ValidationResult.Success;

Upvotes: 1

Related Questions