Possible WPF Bug: Input Field with Invalid Input Not Highlighted On Startup

I am building a WPF demo, and I wanted to include validation. My scenario cannot be simpler: I show a form to create a shipment, and when you click “Save” button it checks the inputs and screams if something is wrong by marking appropriate fields with red. The application uses MVVM.

Finding meaningful WPF examples for this scenario proved to be surprisingly hard.

Apparently, WPF validation does not support this scenario easily. There is no easy way to say: hey, can you please run validation for the whole form now? One can probably look at INotifyDataErrorInfo, but I did not go that far.

So, OK, I am fine with validating things as you go. But then if the form comes up with an empty value for “City” and this is invalid, the “City” field should be immediately highlighted as red, no?

Well, it seems to work fine in .NET 4. In .NET 3.5 SP1, however, the value will become red only after the first change. Validation functions (IDataErrorInfo.Item[“City”]) gets called on form load, and I return “Please enter City”, but the validation framework seems to ignore it. If I just go ahead and hit “Save”, validation framework won’t report any errors.

Occasionally, though, it may also work under .NET 3.5SP1, I have an app that seems to be working just fine. I built a simple repro case that works fine under .NET 4, but fails to validate under .NET 3.5SP1. I could not, however, build a reliable repro case that works under .NET 3.5SP1.

This very simple thing – to validate a form on “GO”, proved to be too elusive, and I definitely don’t like enumerating my property names as strings in some indexer.

Leave a Reply

Your email address will not be published. Required fields are marked *