wpf DataTrigger


Example

A DataTrigger can be attached to any property, be it on it's own control, another control, or even a property in a non UI class. Consider the following simple class.

public class Cheese
{
    public string Name { get; set; }
    public double Age { get; set; }
    public int StinkLevel { get; set; }
}

Which we will attach as the DataContext in the following TextBlock.

<TextBlock Text="{Binding Name}">
    <TextBlock.DataContext>
        <local:Cheese Age="12" StinkLevel="100" Name="Limburger"/>
    </TextBlock.DataContext>
    <TextBlock.Style>
        <Style TargetType="{x:Type TextBlock}">
            <Style.Triggers>
                <DataTrigger Binding="{Binding StinkLevel}" Value="100">
                    <Setter Property="Foreground" Value="Green"/>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </TextBlock.Style>
</TextBlock>

In the preceeding code, the TextBlock.Foreground property will be Green. If we change the StinkLevel property in our XAML to anything other than 100, the Text.Foreground property will revert to it's default value.