Bookmark and Share Share...    Subscribe to this feed Feed   About Christian Moser  


Set a ValidationError by Code

A ValidatioRule in a BindingExpression only triggers, when the target side of the binding changes. If you want to set a validation error by code you can use the following snippet.

Set the validation error

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

Clear the validation error

 
Validation.ClearInvalid(textBox.GetBindingExpression(TextBox.TextProperty));
 
 




Last modified: 2008-12-04 13:14:24
Copyright (c) by Christian Moser, 2011.

 Comments on this article

Show all comments
Christian
Commented on 3.March 2009
Don't have much help for a beginner like me.
Alexandra
Commented on 19.March 2009
Great tip, this helped me a lot! Keep 'em coming!
Yuri
Commented on 19.April 2009
What is it - regexValidationRule?
Gergely...
Commented on 12.November 2009
Regular Expression ... Google.com :)
balwat singh
Commented on 21.April 2010
who to use validation inside WPF.
ashish
Commented on 3.August 2010
wha the fuck is this ?
hi
Commented on 7.August 2010
chethi venna mudda chengal malthadu
yy
Commented on 1.November 2010
superb
xr280xr
Commented on 22.November 2010
Thank you! I searched for almost an hour trying to remember the name of this method. I can't believe it is not more frequently used or referenced. When taking this approach, you obviously know that some validation has failed, so what is the significance of the ValidationRule passed to the ValidationError? What is it used for?
pundarik
Commented on 25.January 2011
it's really help less for beginner dear....!
Max
Commented on 8.February 2011
Very helpful!
Please keep writing such posts :)
ste
Commented on 8.February 2011
this example is orrible!
kashif
Commented on 8.March 2011
it help less for me as m begneer...is there any way to validate in xaml
domondaemon
Commented on 10.March 2011
what an example ya i dont expect this example from you its disappointing
BalaMurugan
Commented on 12.April 2011
yeaha Superu supero Super aapu....
Anju
Commented on 24.June 2011
Thanks Christian.How would you do the same validation in MVVM? Any idea?
Anju
Commented on 24.June 2011
How to do the same validation in MVVM? to check if the textbox is not empty and if the text entered in txtbx is of the proper reg expression. Thanks...
aaaa
Commented on 1.July 2011
really very nice......
shaily
Commented on 9.July 2011
thank you,but for messege display what to do?
Unknown
Commented on 10.August 2011
I don't understand it. but it will be superb.
Beauty
Commented on 13.September 2011
I had problems to find out what's the "regexValidationRule". Then I found a more detailed page with example. LOOK HERE: . . . . . .
http://www.wpftutorial.net/DataValidation.html

Name
E-Mail (optional)
Comment