You do this with Styles and DataTriggers. Just set your ElementStyle with your default background property, in this case Green, and add DataTriggers for the other cases:
<DataGridTextColumn Binding="{Binding WhateverIWantToDisplay}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background" Value="Green" />
<Style.Triggers>
<DataTrigger Binding="{Binding Foo}" Value="1">
<Setter Property="Background" Value="Blue" />
</DataTrigger>
<DataTrigger Binding="{Binding Foo}" Value="2">
<Setter Property="Background" Value="Red" />
</DataTrigger>
<DataTrigger Binding="{Binding Foo}" Value="2">
<Setter Property="Background" Value="Yellow" />
</DataTrigger>
</Style.Triggers>
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
Another approach is to use a binding with a converter:
<DataGridTextColumn Binding="{Binding WhateverIWantToDisplay}" >
<DataGridTextColumn.ElementStyle>
<Style TargetType="{x:Type TextBlock}">
<Setter Property="Background"
Value="{Binding Foo, Converter={x:Static my:FooToColorConverter.Instance}}" />
</Style>
</DataGridTextColumn.ElementStyle>
</DataGridTextColumn>
with this converter:
public class FooToColorConverter : IValueConverter
{
public static readonly IValueConverter Instance = new FooToColorConverter();
public object Convert(object value, ...
{
int foo = (int)value;
return
foo==1 ? Brushes.Blue :
foo==2 ? Brushes.Red :
foo==3 ? Brushes.Yellow :
foo>3 ? Brushes.Green :
Brushes.Transparent; // For foo<1
}
public object ConvertBack(...
{
throw new NotImplementedException();
}
}
Note that answer serge_gubenko gave will work as well, but only if your Foo property value never changes. This is because the Color property getter will only be called once. His solution can be improved by changing Color to a read-only DependencyProperty and updating it whenever Foo is assigned, but it is generally a bad idea to have UI-specific information like colors in your data model, so it is not recommended.