Here is a way to format DataGrid cell foreground color based on the cell content:
1. Create a value converter:
public class NumericConverter: IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
if (value.ToString() == “”)
return value;
return System.Convert.ToDouble(value) %2 == 0 ? Brushes.Black : Brushes.Red;
}
public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
throw new NotImplementedException();
}
}
2. Define a CellStyle as follows:
<Style x:Key="CurrencyCellStyle" TargetType="{x:Type Custom:DataGridCell}"> <Setter Property="Foreground" Value="{Binding RelativeSource={RelativeSource Self}, Path=Content.Text, Converter={StaticResource NumericConverter}}" /> </Style>
3. Apply it to the desired DataGridTextColumn:
<Custom:DataGridTextColumn Header="Number" CellStyle="{StaticResource CurrencyCellStyle}" Binding="{Binding Number}"/>