Saturday 1 October 2011

Validation? We don't need no validation.

I decided to give WPF a go last week, after all, it's only been out for five years. I wrote a small app that writes to the configuration file of a windows service. This app ensures that the fields are validated and also that the app.config file is well formed (we've had a few instances were an web.config file has been edited manually and the values placed after the configuration closing tags) or at least that was the idea.

Anyway, in the old days of winforms apps, in order to validate a textbox all one had to do was implement the Validating event. If it passed validation, the program would continue otherwise the event would be cancelled and focus stayed with the same textbox.

Imagine my surprise when the validating event wasn't there for a WPF textbox. I thought that it must be called something else, i.e. there would be an event that would have similar functionality, but alas there isn't. It is possible to use the previewtextinput event to prevent text being input, e.g. letters in a numerical only field, but there are limitations to this approach, as it only validates one character.

This should be another, why Microsoft why post? There are a few solutions but they involve writing quite a bit of code, which is fine, but it seems like a step backwards to me.

No comments:

Post a Comment