0

I came across Set a ValidationError by Code, by Christian Mosers

ValidationError validationError = new ValidationError(regexValidationRule, 
    textBox.GetBindingExpression(TextBox.TextProperty));

validationError.ErrorContent = "This is not a valid e-mail address";

Validation.MarkInvalid(textBox.GetBindingExpression(TextBox.TextProperty), 
    validationError);

How can I use this in my ViewModel?

1 Answer 1

3

There are other ways to get errors out of the VM, for example by implementing IDataErrorInfo or by throwing exceptions in setters (the binding then needs to validate on exception).

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks and +1 for you answer, I was aware of the IDataErrorInfo (should've mentioned that in my question). If you get a chance could you also provide comments or some code showing how to use the technique from Christina Mosers with regards to the question?
@VoodooChild: As far as i am aware you should not have a reference to UI-elements in your VM, and the approach mentioned requires you to have a reference to the UI-element which displays your data. That being the case i do not think it is suited for this pattern.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.