Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set the Background of a DataGridRow based on the content of a cell

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

like image 927
Phil Gan Avatar asked Jan 27 '11 20:01

Phil Gan


1 Answers

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.

like image 62
Pavlo Glazkov Avatar answered Nov 15 '22 06:11

Pavlo Glazkov