Majed DH
Majed DH

Reputation: 119

Change Background for whole bound row in datagrid

i have a datagrid in wpf project , using manual columns

<DataGridTextColumn Binding="{Binding FirstName}" />
<DataGridTextColumn   Binding="{Binding LastName}"/>
<DataGridTextColumn  Binding="{Binding Society}"/>
<DataGridTemplateColumn>
    <DataGridTemplateColumn.CellTemplate>
        <DataTemplate>
           ....
        </DataTemplate>
    </DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

I want to change the background of the row depending on the bound data in each row,

any way to do it ?

Upvotes: 0

Views: 65

Answers (2)

Sankarann
Sankarann

Reputation: 2665

As the Same thing what you need is already discussed in DataGrid selection and conditional formatting...

It was generally say as Conditional Formatting.. you can either format the cell or a row based on some conditions..

Upvotes: 0

dkozl
dkozl

Reputation: 33384

You can change default Style for DataGridCell to set Background colour from bound property via custom IValueConverter that would convert your property value into Brush.

<DataGrid.Resources>
   <Style TargetType="{x:Type DataGridCell}">
      <Setter Property="Background" Value="{Binding Path=SomeProperty, Converter={StaticResource ValueToColourConverter}}"/>
   </Style>
</DataGrid.Resources>

or if you want to change Background only when that property has some specific value then you can use Style.Triggers:

<DataGrid.Resources>
   <Style TargetType="{x:Type DataGridCell}">
      <Style.Triggers>
         <DataTrigger Binding="{Binding Path=SomeProperty}" Value="SomeValue">
            <Setter Property="Background" Value="Yellow"/>
         </DataTrigger>
      </Style.Triggers>
   </Style>
</DataGrid.Resources>

Upvotes: 1

Related Questions