By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,495 Members | 1,329 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,495 IT Pros & Developers. It's quick & easy.

Change the Background Color of the DataGridRow using DataTriggers

P: 10
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.

Expand|Select|Wrap|Line Numbers
  1. <DataGridTextColumn.ElementStyle>
  2.    <Style TargetType="{x:Type TextBlock}">
  3.       <Style.Triggers>
  4.          <Trigger Property="Text" Value="4">
  5.             <Setter Property="Background" Value="LightGreen"/>
  6.          </Trigger>
  7.       </Style.Triggers>
  8.    </Style>
  9. </DataGridTextColumn.ElementStyle>
Nov 14 '12 #1

✓ answered by Frinavale

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:
Expand|Select|Wrap|Line Numbers
  1. <DataGrid.Resources>
  2.     <Style TargetType="DataGridRow">
  3.         <Style.Triggers>
  4.             <DataTrigger Binding="{Binding Status}" Value="4">
  5.                 <Setter Property="Background" Value="Green"/>
  6.             </DataTrigger>
  7.  
  8.             <DataTrigger Binding="{Binding Status}" Value="3">
  9.                 <Setter Property="Background" Value="Red"/>
  10.             </DataTrigger>
  11.         </Style.Triggers>
  12.     </Style>
  13. </DataGrid.Resources>
-Frinny

Share this Question
Share on Google+
2 Replies


Frinavale
Expert Mod 5K+
P: 9,731
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:
Expand|Select|Wrap|Line Numbers
  1. <DataGrid.Resources>
  2.     <Style TargetType="DataGridRow">
  3.         <Style.Triggers>
  4.             <DataTrigger Binding="{Binding Status}" Value="4">
  5.                 <Setter Property="Background" Value="Green"/>
  6.             </DataTrigger>
  7.  
  8.             <DataTrigger Binding="{Binding Status}" Value="3">
  9.                 <Setter Property="Background" Value="Red"/>
  10.             </DataTrigger>
  11.         </Style.Triggers>
  12.     </Style>
  13. </DataGrid.Resources>
-Frinny
Nov 14 '12 #2

P: 10
yeah that is what i need!!!! many thx Frinny for your example!!!! It works fantastic!!!
Nov 15 '12 #3

Post your reply

Sign in to post your reply or Sign up for a free account.