I want to change the cell content text font and color based on it's value but I need to add the columns from code. The problem is that the table shows me HEX values instead of colorizing the cell values.
I added in XAML resources:
<Style x:Key="MyStyle" TargetType="{x:Type TextBlock}">
<Setter Property="Foreground" Value="{Binding TextColor}" />
</Style>
and the following code lines initializes the columns:
DataGridTextColumn column = new DataGridTextColumn();
column.Header = field.name;
column.Binding = new Binding(field.name)
{
Mode = BindingMode.TwoWay,
UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged,
Converter = new NameToBrushConverter()
};
column.ElementStyle = this.FindResource("MyStyle") as Style;
dgwDataMain.Columns.Add(column);
My custom function:
public class NameToBrushConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
{
return ((string)value == "asd") ? Brushes.Red : Brushes.Black;
}
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
{
return Binding.DoNothing;
}
}