I have a simple standard WPF 4 DataGrid with two columns.
<DataGrid ItemsSource="{Binding Source={StaticResource ItemDataView}}" AutoGenerateColumns="False">
<DataGrid.Columns>
<DataGridTemplateColumn Header="Alpha">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Alpha}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
<DataGridTemplateColumn Header="Beta">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Beta}" />
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>
</DataGrid.Columns>
</DataGrid>
The data objects displayed are defined as so:
public class MyObject : INotifyDataErrorInfo
{
[Required]
public string Alpha { get; set; }
public string Beta { get; set; }
public bool HasErrors
{
get { return string.IsNullOrEmpty(Alpha); }
}
public event EventHandler<DataErrorsChangedEventArgs> ErrorsChanged;
public IEnumerable GetErrors(string propertyName)
{
yield return propertyName;
}
}
i.e. Alpha must have a non-empty value, but Beta can be null.
If I add a MyObject where the Alpha value is empty, then both the Alpha and Beta cells are given a red border indicating a validation error. This only happens for DataGridTemplateColumns; I've tried adding a DataGridTextColumn and it doesn't get the red border. The Beta column also gets a red border if it is not bound to anything, or even if it doesn't have a control attached (e.g. DataTemplate is empty).
a) Why does this happen?
b) How can I stop it happening? I only want the red validation border on the cell with the error.