0

I have a label bound to a integer value.

If this label is zero then I would like the label colour white, but if the value is greater then zero I would like the label appearing to flash/blink and be red.

Is this possible, and if so, how would I achieve this?

Many thanks

  • You're going to need a value converter and some animation. You'll get a million hits on value converters on Google, animation code here: http://stackoverflow.com/questions/2652831/blinking-textblock . – goobering Jul 27 '15 at 16:07

1 Answers1

1

Converter:

class IsGeaterThanZeroToBoolConverter : IValueConverter
{
    public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        return (int) value > 0;
    }

    public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
    {
        throw new NotImplementedException();
    }
}

XAML:

<Window.Resources>
    <local:IsGeaterThanZeroToBoolConverter x:Key="IsGeaterThanZeroToBoolConverter"/>
</Window.Resources>

<Label Content="{Binding Age}">
    <Label.Style>
        <Style TargetType="Label">
            <Style.Triggers>
                <DataTrigger Binding="{Binding Age}" Value="0">
                    <Setter Property="Foreground" Value="White"/>
                </DataTrigger>
                <DataTrigger Binding="{Binding Age, Converter={StaticResource IsGeaterThanZeroToBoolConverter}}" Value="True">
                    <Setter Property="Foreground" Value="Red"/>
                    <DataTrigger.EnterActions>
                        <BeginStoryboard>
                            <Storyboard>
                                <DoubleAnimation Storyboard.TargetProperty="Opacity" AutoReverse="True"
                                                To="0" RepeatBehavior="Forever"/>
                            </Storyboard>
                        </BeginStoryboard>
                    </DataTrigger.EnterActions>
                </DataTrigger>
            </Style.Triggers>
        </Style>
    </Label.Style>
</Label>
Maximus
  • 3,458
  • 3
  • 16
  • 27