我在 TextBox 上设置绑定,并且文本不能为空白(null、空格或其他)。我设置了一个 ValidationRule 如下:
internal class CannotBeBlankTextboxRule : ValidationRule
{
public String ErrorText { get; set; } = "The text cannot be blank.";
public override ValidationResult Validate (Object value, CultureInfo cultureInfo)
{
var str = value as String;
if (String.IsNullOrWhiteSpace(str))
return (new ValidationResult(false, this.ErrorText));
return (new ValidationResult(true, null));
}
}
它工作正常,但该窗口允许用户单击其他地方并继续,即使存在此错误。
验证失败时,有没有办法让 TextBox 不失去焦点?