So I have a WPF DataGrid
, which is bound to an ObservableCollection
. The collection has validation on its members, through IDataErrorInfo
. If I edit a cell in a way so as to be invalid, and then tab away from it before hitting enter, then come back and make it valid, the cell will stop showing invalid, however, the "!" at the head of the row will still be there, and the ToolTip
will reference the previous, invalid value.
WPF DataGrid validation errors not clearing
datagrididataerrorinfovalidationwpfwpfdatagrid
Best Answer
Not using
Mode=TwoWay
forDataGridTextColumns
solves one version of the problem, however it seems that this problem can appear out of nowhere for other reasons as well.(Anyone who has a good explanation as of why not using
Mode=TwoWay
solves this in the first place is probably close to a solution to this problem)The same thing just happened to me with a
DataGridComboBoxColumn
so I tried to dig a little deeper.The problem isn't the
Binding
in theControl
that displays theErrorTemplate
insideDataGridHeaderBorder
. It is binding itsVisibility
toValidation.HasError
for the ancestorDataGridRow
(exactly as it should be doing) and that part is working.The problem is that the validation error isn't cleared from the
DataGridRow
once it is resolved. In my version of the problem, theDataGridRow
started out with 0 errors. When I entered an invalid value it got 1 error so, so far so good. But when I resolved the error it jumped up to 3 errors, all of which were the same.Here I tried to resolve it with a
DataTrigger
that set theValidationErrorTemplate
to{x:Null}
ifValidation.Errors.Count
wasn't 1. It worked great for the first iteration but once I cleared the error for the second time it was back. It didn't have 3 errors anymore, it had 7! After a couple of more iterations it was above 10.I also tried to clear the errors manually by doing
UpdateSource
andUpdateTarget
on theBindingExpressions
but no dice.Validation.ClearInvalid
didn't have any effect either. And looking through the source code in the Toolkit didn't get me anywhere :)So I don't have any good solutions to this but I thought I should post my findings anyway..
My only "workaround" so far is to just hide the
ErrorTemplate
in theDataGridRowHeader