Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(Temporarily) Disable Validation In WPF

Tags:

validation

wpf

When a class implements the IDataErrorInfo interface and this class is part of a binding to a form, is there a way to temporarily disable the validation associated with the binding?

For example, if I do not want to allow empty strings in a property of my class, when the form opens the bound control is flagged as having an error (with a red border by default). I think it is a bit clunky to immediately flag the input as having an error (before the user even has a chance to enter something). Is there a way around this?

I have searched most of the afternoon but what I seem to be coming up with is how to disable the submit button until all input is valid (which is not really what I am after).

like image 336
Jason Richmeier Avatar asked Sep 13 '12 21:09

Jason Richmeier


1 Answers

Have a flag in the class that implements the IDataErrorInfo which controls when the validation is done e.g. DoValiation;

When the flag is false you would make IDataErrorInfo report that there are no errors (i.e. return null, etc).

During view intialisation you would set the DoValidation flag to false in your model then let the view do all the Bindings to your data.

After the view is "Loaded" you would then set the DoValidation flag to true....from that point on...changes to the data will cause the error indicators to appear if the values are still invalid.

See this other post for more detail on how to structure your IDataErrorInfo code:

  • WPF UpdateSourceTrigger
like image 147
CSmith Avatar answered Oct 01 '22 02:10

CSmith