Search code examples
wpfmvvmidataerrorinfo

How can I stop IDataErrorInfo from firing for new items?


I am developing a WPF application (using MVVM) and have implemented IDataErrorInfo on my ViewModel. Part of my validation checks that mandatory fields have been entered. This works perfectly for editing existing records but doesn't provide a nice user experience when adding new records. As soon as I load the new view, the mandatory fields are highlighted as invalid.

Is there a (preferably non-hacky) solution to this? It seems a pretty standard thing to want, so I'm hoping I'm missing something simple.


Solution

  • Use a flag that indicates whether the record is new. Check this flag in your implementation of IDataErrorInfo, and set the flag to true the first time the user tries to validate the input.