wpf DataTrigger


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}">
        <local:Cheese Age="12" StinkLevel="100" Name="Limburger"/>
        <Style TargetType="{x:Type TextBlock}">
                <DataTrigger Binding="{Binding StinkLevel}" Value="100">
                    <Setter Property="Foreground" Value="Green"/>

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.