Change the Background Color of the DataGridRow using DataTriggers

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Mathias123
    New Member
    • Nov 2012
    • 10

    #1

    Change the Background Color of the DataGridRow using DataTriggers

    Hello!

    i have a problem. I have a WPF Datagrid (.net 4) binding with 10 columns. the last column is binding with Status. Now i want to set the row colour to green if status = 4 or red if status = 3

    can somebody help me... i only found this XAML Code but this is only for one cell.

    Code:
    <DataGridTextColumn.ElementStyle>
       <Style TargetType="{x:Type TextBlock}">
          <Style.Triggers>
             <Trigger Property="Text" Value="4">
                <Setter Property="Background" Value="LightGreen"/>
             </Trigger>
          </Style.Triggers>
       </Style>
    </DataGridTextColumn.ElementStyle>
    Last edited by zmbd; Nov 14 '12, 01:34 PM. Reason: [Z(Please use the <CODE/> button to format posted code)]
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    To set the background style for the row, create a style that targets the DataGridRow type as a resource for your DataGrid and use DataTriggers to set the background property of the row depending on the status of the object you're binding to.

    Like this:
    Code:
    <DataGrid.Resources>
        <Style TargetType="DataGridRow">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Status}" Value="4">
                    <Setter Property="Background" Value="Green"/>
                </DataTrigger>
    
                <DataTrigger Binding="{Binding Status}" Value="3">
                    <Setter Property="Background" Value="Red"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </DataGrid.Resources>
    -Frinny
    Last edited by Frinavale; Nov 14 '12, 09:06 PM.

    Comment

    • Mathias123
      New Member
      • Nov 2012
      • 10

      #3
      yeah that is what i need!!!! many thx Frinny for your example!!!! It works fantastic!!!

      Comment

      Working...