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.
相关问题
- VNC control for WPF application
- Date with SimpleDateFormat in Java
- WPF Binding from System.Windows.SystemParameters.P
- XAML: Applying styles to nested controls
- How can I add a horizontal line (“goal line”) for
My scenario was like this:
IDataErrorInfo
Custom Row Validation rule based on WPF DataGrid Practical Examples -Validation with IDataErrorInfo , that combined all errors from Model using IDataErrorInfo.
ValidatesOnDataErrors=True
,ValidatesOnExceptions=True
,NotifyOnValidationError=True
within the binding (which I started with)This caused multiple access to my validation Engine and eventualy left my
DataGrid
in inconsistent state (Error notification on row header even when row Valid).The solution was to remove switches from the binding (point 3.)
I suggest reading through Clearing a DataGrid row validation error too.
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
If you are seeing an increasing number of errors similar to Meleak, I would be interested to know how your error collection gets populated. In Meleaks version of the problem, he sees three errors (and more) after resolving the invalid data.
In my Data Validation code, I remove the previous instance of a particular error then re-add every time the data changes. For reference, here is a sample:
The Validation Plumbing
A Property Being Validated
So, when the property Changed handler is run, if an error exists in the ValidationErrors dictionary, it is removed, then the value is checked, and it if does not match requirements, an error is added to the dictionary. This helps ensure that only one instance of any error is present in that entities validation error dictionary.
My workaround was not to use Validation.Errors, but use DataGridRow.Item property. If your DataGrid is bound to business objects which implement IDataErrorInfo interface, then you can add IsNotValid property (or IsValid), and make sure Error property returns all errors associated with the object. Then customize default style for DataGridRowHeader:
Also in DataGridRow style customize ValidationErrorTemplate, so that it shows error message from DataGridRow.Item.Error proeprty.
My workaround was to simply remove the property UpdateSourceTrigger="LostFocus" from the binding declaration in each datagridcolumn.
try removing the
Mode=TwoWay
for each of theDataGridTextColumns
from each of the Binding elements.