Search code examples
wpfmvvmforeground

WPF blinking foreground label


I'm trying to make the foreground of a label blink. I tried the following code but I get the following exception and I don't know how to solve it.

'System.Windows.Media.Animation.ColorAnimation' animation object cannot be used 
 to animate property 'Foreground' because it is of incompatible type 
'System.Windows.Media.Brush'.

Using this XAML:

 <Label Content="{Binding Path=SendingAlert, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                       Foreground="Transparent"
                       HorizontalAlignment="Right">
                    <Label.Style>
                        <Style>
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Path=IsSending, Mode=OneWay, UpdateSourceTrigger=PropertyChanged}"
                                             Value="True">
                                    <DataTrigger.EnterActions>
                                        <BeginStoryboard>
                                            <Storyboard
                                                    Storyboard.TargetProperty="Foreground"
                                                    Duration="0:0:0.5">
                                                <ColorAnimation From="Transparent" To="Red" AutoReverse="True" RepeatBehavior="Forever"/>
                                            </Storyboard>
                                        </BeginStoryboard>
                                    </DataTrigger.EnterActions>
                                </DataTrigger>
                                <!--<DataTrigger Binding="{Binding IsSending}" Value="False">
                                    <Setter Property="Foreground" Value="Transparent"/>
                                </DataTrigger>-->
                            </Style.Triggers>
                        </Style>
                    </Label.Style>
                </Label>

and where

 public bool IsSending
    {
        get { return !CanDoActions; }
    }

    private string _sendingAlert = "sending";//string.Empty;
    public string SendingAlert
    {
        get { return _sendingAlert; }
        set
        {
            _sendingAlert = value;
            OnPropertyChanged(() => SendingAlert);
        }
    }

Any idea of how to fix this?


Solution

  • The Foreground property is of type Brush, which is different from an object of type Color

    You can use a ColorAnimiation to animate an object of type Color, but not of type Brush, so to animate your foreground brush, you need to set the property to the Brush.Color, like this:

    Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"