5
votes

I have a DataGrid with column named Color.

<DataGridTextColumn Header="Color" Binding="{Binding MyColor.Percentage}"/>

The ItemSource of DataGrid is some object with MyColor property inside.

public class MyColor
{
    Color Background { get; set; }
    int Percentage { get; set; }
}

When ItemSource is set column auto-fills with values of Percentage. Now I'd like to set background of each cell in this column to color corresponding to MyColor.Color property. Is there a way to do it using binding? Something like

Background="{Binding MyColor.Color}"

Color property is in html format #XXXXXXXX (is it called html format?).

1

1 Answers

10
votes

You can set it via CellStyle:

<DataGridTextColumn Header="Color" Binding="{Binding MyColor.Percentage}">
    <DataGridTextColumn.CellStyle>
        <Style TargetType="DataGridCell">
            <Setter Property="Background" Value="{Binding MyColor.Background}" />
        </Style>
    </DataGridTextColumn.CellStyle>
</DataGridTextColumn>

Also you have to change your MyColor class to have a Background property with type Brush, not Color. Or you can use a converter to convert Color into SolidColorBrush.