0

I have a model class that implements INotifyDataErrorInfo using a pattern similar to this article (although mine isn't async). One of the properties has a Range DataAnnotation attribute on it. When I run the application and enter an invalid value into the bound TextBox, the UI looks like this:-

enter image description here

Not only is a red border drawn around the TextBox (the red triangle is part of a custom style), but a red border also appears around the entire DataTemplate for this model type. Why is the latter happening, and how can I stop it?

Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152
  • Are you raising ErrorsChanged with null / emptystring as the property name? – canton7 Feb 10 '15 at 14:47
  • @canton7 no, a breakpoint in OnErrorsChanged shows that the property name is always present in the event args. – Andrew Stephens Feb 10 '15 at 14:56
  • Is GetErrors ever called with null / emptystring? – canton7 Feb 10 '15 at 15:00
  • @canton7 yes, after changing the TextBox value, GetErrors() gets called twice - once with the property name and once with an empty string. The latter results in all errors being returned by the method (which seems to be what all INotifyDataErrorInfo implementations do), and is presumably the WPF framework asking for all "entity level" errors. Returning null instead solves my problem, but it begs the question why none of the many implementations do this? – Andrew Stephens Feb 10 '15 at 15:08
  • Post your xaml code.. – Ayyappan Subramanian Feb 10 '15 at 15:38
  • 1
    @AndrewStephens sorry for the delay - got distracted. The MSDN docs clearly state that GetErrors with null / emptystring should return entity-level errors. It looked to me that the unwanted red box was highlighting the entire entity, which is what prompted me to ask. It looks like "all INotifyDataErrorInfo implementations" implement this wrong (blind copying the blind, yay), and it doesn't normally pop up because people aren't validating models which appear in DataTemplates, maybe? – canton7 Feb 10 '15 at 16:06

1 Answers1

0

After being prompted to look into the code by @canto7's comments, and doing a bit more digging, I think I see the problem now. There seems to be some confusion over what entity-level errors means, such as this SO question.

The INDEI implementation that I've used (and a lot of others I suspect) interpret this as "return every validation error for every property". In reality I think entity-level errors means the exact opposite - they are validation errors that have not been recorded against specific properties. For example some complex validation rule that involves multiple properties and/or other classes.

It all comes down to what your requirements are. It may be sufficient to record just the property-specific errors, in which case GetErrors(empty string) should return null. However if you do need to record "entity-level" validation errors then you will need to implement this functionality (most INDEI articles don't), and return these errors from GetErrors(empty string).

Just bear in mind that returning errors in response to this may have undesirable UI effects, as I have found.

Community
  • 1
  • 1
Andrew Stephens
  • 9,413
  • 6
  • 76
  • 152