Question

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;
        }
    }
Was it helpful?

Solution

You can achieved this with MultiValueConverter.

Example:

DataGridTextColumn column = new DataGridTextColumn();
column.Header = "Name";
column.Binding = new Binding("Name")
{
      Mode = BindingMode.TwoWay,
      UpdateSourceTrigger = UpdateSourceTrigger.PropertyChanged                
};
column.ElementStyle = this.FindResource("MyStyle") as Style;
grid.Columns.Add(column);

List<Foo> _source = new List<Foo> 
{
        new Foo{ Name ="test1"},
        new Foo{ Name ="test2"},
        new Foo{ Name ="test3"}
};

grid.ItemsSource = _source;

Foo class:

class Foo
{
   public string Name { get; set; }       
}

Style XAML:

<local:NameToBrushConverter x:Key="nameToBC" />

<Style x:Key="MyStyle" TargetType="{x:Type TextBlock}">
   <Setter Property="Foreground" >
            <Setter.Value>
                 <MultiBinding Converter="{StaticResource nameToBC}" >                       
                       <Binding Path="." />                      
                 </MultiBinding>
             </Setter.Value>
    </Setter>
</Style>

MultiValueConverter code:

 class NameToBrushConverter : IMultiValueConverter
    {
        public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
        {
            Foo item = values[0] as Foo;
            if (item != null)
            {               
                if (item.Name == "test2")
                    return Brushes.Red;
                else
                    return Brushes.Black;
            }

            return Brushes.Black;
        }
        public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top