1

我正在尝试使标签的前景闪烁。我尝试了以下代码,但出现以下异常,我不知道如何解决。

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

使用此 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>

和在哪里

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

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

知道如何解决这个问题吗?

4

1 回答 1

1

Foreground属性是 type Brush,它不同于 type 的对象Color

您可以使用 aColorAnimiation为 type 对象设置动画Color,但不是 type Brush,因此要为前景画笔设置动画,您需要将属性设置为Brush.Color,如下所示:

Storyboard.TargetProperty="(Foreground).(SolidColorBrush.Color)"
于 2013-05-09T19:44:59.273 回答