I want to achieve a pretty simple task, but none of the solutions here on SO or otherwise found have helped me so far:
I have a WPF DataGrid, which is b
This seems a near duplicate of this [question]: (Checking If Any WPF DataGrid Cell Has Error).
Here's an answer that's more XAMly. It relies on the fact that when an error template is shown its IsVisible property toggles.
Just create a ValidationErrorTemplate for the DataGridRow [1]: (http://msdn.microsoft.com/en-us/library/ee622975.aspx)
Create 2 attacheded properties. The value of the one attached property is the DataGrid itself,
local:DataGridValidator.DataGridToMarkForValidationErrors="{Binding RelativeSource={RelativeSource AncestorType=DataGrid}}"
Attach the property to the root control of the ValidationErrorTemplate.
In the callback for the attached property set the 2nd property on the DataGrid to show whether it has errors:
private static void DataGridPropertyChangedCallback(DependencyObject dependencyObject, DependencyPropertyChangedEventArgs args)
{
var fe = dependencyObject as FrameworkElement;
var target = args.NewValue as DependencyObject;
if (fe != null && target != null)
{
target.SetValue(IsFreeOfValidatoionErrorsProperty, !fe.IsVisible);
fe.IsVisibleChanged += (_1, _) =>
{
target.SetValue(IsFreeOfValidatoionErrorsProperty, !fe.IsVisible);
};
}
Now the DataGrid has a property (IsFreeOfValidatoionErrorsProperty) showing whether it has errors or not. I made the property default positive so I could easily AND it with my VM IsDirty/CanSave property.