6

Is there a way, using XAML, to dynamically set the background of a row based on the content of one of it's cells?

Thanks,

Phil

Phil Gan
  • 2,703
  • 2
  • 27
  • 37

1 Answers1

18

You can define a style for a row and change the color using DataTrigger. Something like this:

<DataGrid>
    <DataGrid.RowStyle>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding BooleanPropertyOnObjectBoundToRow}" Value="True">
                   <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.RowStyle>
</DataGrid>

Here BooleanPropertyOnObjectBoundToRow is a boolean property on your data object one the cells is bound to.

Pavlo Glazkov
  • 19,780
  • 3
  • 56
  • 67
  • How do I make the color dynamic instead of just Red? – Phil Gan Jan 27 '11 at 21:08
  • Sorry, maybe I've misunderstood. My property is actually an `Enum` I guess I can make an enum to boolean converter for the binding. – Phil Gan Jan 27 '11 at 21:10
  • 6
    @Phil: You also can specify your own enumeration values for the Value-proeprty of the Binding. For this you must declare the namespace of your enumeration and the set it in the value attribute with Value="{x:Static yourNamespace:YourEnum.YourValue}" – HCL Jan 27 '11 at 21:16
  • Thanks, I would never have figured that out alone. – Phil Gan Jan 27 '11 at 21:25
  • Not sure I understand this: Binding="{Binding BooleanPropertyOnObjectBoundToRow}" what is this? – RSM Dec 18 '13 at 13:05