Checking for validity of an entire grid

May 19, 2009 at 12:34 PM


I am struggling with this one for a very long time. I could not find a proper answer anywhere and the most puzzling of all is that nobody seems to have come across the same issue.

To put things really simple, I want to be able to know if the grid has a validation error anywhere. The ideal way is to have a dependency property for this. There appears to be a property that does exactly this - HasRowValidationError. But unfortunately it is both private and not a DP. Why?

I have tried utilizing various datagrid events/virtual methods to achieve this functionality but I failed miserably. Nothing too useful there for my purposes.

I have also tried taking advantage of Validation.HasError attached dependency property on a DataGridRow with limited success. Using styles I have managed to propagate a value of this property for any row up to a custom attached property on the datagrid itself, but this didn't work as expected. I observed a pattern of True,False,True on a Validation.HasError property on a DataGridRow. The grid's ItemsSource is bound to a Collection<Item> where Item is some business class. IEditableObject is not implemented. When a new item is added to the grid it contains some defaults which are immediately rendered invalid by my custom ValidationRule.

I nailed the problem down to OnExecutedCancelEdit and a portion of code that calls CancelEdit() and UpdateSources() on a BindingGroup. Basically, if I step through the DataGrid's code immediately before calling CancelEdit() and UpdateSources() on a BindingGroup, the Validation.HasError attached property is True. After stepping over UpdateSources(), Validation.HasError first goes back to False, then my ValidationRule is called once again, and since my data is still invalid (even though we are cancelling), the Validation.HasError goes back to True. So even though we have cancelled the edit, the row's Validation.HasError remains True.

This behavior does not allow me to use Validation.HasError to detect grid errors and a property that is already implemented for this purpose is private.

Is there a way to solve this without resorting to implementing IEditableObject?

Any help for this poor soul? Thanks!