Walter Williams
Walter Williams

Reputation: 974

Want ValidationRule to keep focus

I am setting up a binding on a TextBox, and the text cannot be blank (null, whitespace or otherwise). I set up a ValidationRule as below:

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));
    }
}

It is working fine, but the window allows the user to click elsewhere and continue even though this error is there.

Is there a way I can keep the TextBox from losing focus while the validation is failing?

Upvotes: 0

Views: 43

Answers (0)

Related Questions