Change DataGrid Cell Style Based on Condition in WPF

Introduction

some time we need to change the cell background and foreground color based on the binding value. this type of requirements are comes in the like trading application or project. you can change the bacground color by sing Cell Style and Triggers. this blog shows the code for the same.

<DataGridTextColumn.CellStyle>

  <Style TargetType="{x:Type DataGridCell}">
    <
Style.Triggers>
      <
DataTrigger Binding="{Binding ColorBIDQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="1">
        <
Setter Property="Background" Value="Green" />
      </
DataTrigger>
      <
DataTrigger Binding="{Binding ColorBIDQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="2">
        <
Setter Property="Background" Value="Black" />
      </
DataTrigger>
      <
DataTrigger Binding="{Binding ColorBIDQty, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" Value="3">
        <
Setter Property="Background" Value="Red" />
      </
DataTrigger>
    </
Style.Triggers>
  </
Style>
</
DataGridTextColumn.CellStyle>

In the above code when binding value like ColorBIDQty is equal to 1 then the background color will be Green, when 2 the cell background color will be Black and cell background color will be Red when ColorBIDQty is 3

Next Recommended Reading Change Row Style of Data Grid in WPF