Set the Background of a DataGridRow based on the content of a cell
Asked Answered
E

1

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

Emanuele answered 27/1, 2011 at 20:51 Comment(0)
G
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.

Gasoline answered 27/1, 2011 at 21:5 Comment(5)
How do I make the color dynamic instead of just Red?Emanuele
Sorry, maybe I've misunderstood. My property is actually an Enum I guess I can make an enum to boolean converter for the binding.Emanuele
@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}"Alitta
Thanks, I would never have figured that out alone.Emanuele
Not sure I understand this: Binding="{Binding BooleanPropertyOnObjectBoundToRow}" what is this?Unmixed

© 2022 - 2024 — McMap. All rights reserved.