[英]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: 我输入的值通过了我的自定义验证检查,但是WPF仍然显示失败消息:
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);
对于“ 50%”的值,它解析0.5,逐步进入最后一行, return new ValidationResult(true, null);
However, it still causes a failed validation, with a default message "Value '50 %' could not be converted.". 但是,它仍然会导致验证失败,并显示默认消息“无法转换值'50%'。”。
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: 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 似乎只是
In .NET Framework: 在.NET Framework中:
return ValidationResult.ValidResult;
In .NET Core: 在.NET Core中:
return ValidationResult.Success;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.