I've a datagrid bound to datatable. Number of columns are decided during runtime. I'm using Datagrid.RowValidationRule to validate contents of cell after edit. A style is used to highlight the row with red border in case of error.
Is it possible to highlight only the cells having invalid data with red border instead of highlighting complete row?
Binding:
ItemsSource="{Binding Path=., ValidatesOnExceptions=True, NotifyOnValidationError=True, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged, ValidatesOnDataErrors=True}"
AutoGenerateColumns="True"
Validation:
<DataGrid.RowValidationRules>
<local:RowDataValidationRule ValidationStep="UpdatedValue" ValidatesOnTargetUpdated="True"/>
</DataGrid.RowValidationRules>
Style:
<Style x:Key="RowStyle" TargetType="{x:Type DataGridRow}">
<Style.Triggers>
<Trigger Property="Validation.HasError" Value="true">
<Setter Property="BorderThickness" Value="2"/>
<Setter Property="BorderBrush" Value="Red"/>
<Setter Property="ToolTip"
Value="{Binding RelativeSource={RelativeSource Self},
Path=(Validation.Errors)[0].ErrorContent}"/>
</Trigger>
</Style.Triggers>
</Style>
CodeBehind:
public class RowDataValidationRule : ValidationRule
{
public override ValidationResult Validate(object value, CultureInfo cultureInfo)
{
BindingGroup group = (BindingGroup)value;
StringBuilder error = null;
foreach (var item in group.Items)
{
DataRowView rowView = item as DataRowView;
if (rowView != null)
{
// Validation logic, sets error
}
}
if (error != null)
return new ValidationResult(false, error.ToString());
return ValidationResult.ValidResult;
}
}