WPF标签闪烁取决于内容

时间:2015-07-27 15:45:46

标签: wpf xaml

我有一个绑定到整数值的标签。

如果此标签为零,那么我希望标签颜色为白色,但如果该值大于零,我希望标签显示为闪烁/闪烁并为红色。

这是否可能,如果可行,我将如何实现这一目标?

非常感谢

1 个答案:

答案 0 :(得分:1)

<强>转换器:

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>