20

I have a observable collection that binds to a data grid. I also have in the view model a color property and I want to bind the background of each row in the data grid to the color property on the vm.

Scott Boettger
  • 3,657
  • 2
  • 33
  • 44
david
  • 331
  • 2
  • 5
  • 12

1 Answers1

35

You can bind the Background in the RowStyle for DataGrid

<DataGrid ...>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Setter Property="Background" Value="{Binding MyBackground}"/>
        </Style>
    </DataGrid.RowStyle>
    <!-- ... -->
</DataGrid>

This will work if MyBackground is a Brush. You mention in your question that you have a Color, if this is the case you can use this instead

<Setter Property="Background">
    <Setter.Value>
        <SolidColorBrush Color="{Binding MyColor}"/>
    </Setter.Value>
</Setter>
Fredrik Hedblad
  • 83,499
  • 23
  • 264
  • 266