ColorAnimation改变矩形颜色

时间:2014-06-12 12:37:09

标签: c# wpf animation

我想要更改矩形颜色,但是,颜色不会#39;要更改。哪里有问题?

ColorAnimation col = new ColorAnimation();
col.From = Colors.Aqua;
col.To = Colors.PaleGreen;
col.Duration = new Duration(TimeSpan.FromSeconds(1));

Storyboard zgo = new Storyboard();
Storyboard.SetTarget(col, c);
Storyboard.SetTargetProperty(col,
           new PropertyPath(SolidColorBrush.ColorProperty));
zgo.Children.Add(col);
zgo.Begin();

这是XAML:

<Rectangle Name="rec"
    Height="100" Width="200"
    HorizontalAlignment="Left" VerticalAlignment="Top"
    Stroke="Black"
    MouseLeftButtonDown="rec_MouseLeftButtonDown">
    <Rectangle.Fill>
        <SolidColorBrush x:Name="c"></SolidColorBrush>
    </Rectangle.Fill>
</Rectangle>

1 个答案:

答案 0 :(得分:4)

通过调用Animatable.BeginAnimation直接运行动画:

var colorAnimation = new ColorAnimation(
    Colors.Aqua, Colors.PaleGreen, TimeSpan.FromSeconds(1));

c.BeginAnimation(SolidColorBrush.ColorProperty, colorAnimation);

你仍然可以通过使用Rectangle作为动画的目标和Fill.Color作为目标属性::

来实现故事板
Storyboard storyboard = new Storyboard();
Storyboard.SetTarget(colorAnimation, rec);
Storyboard.SetTargetProperty(colorAnimation, new PropertyPath("Fill.Color"));
storyboard.Children.Add(colorAnimation);
storyboard.Begin();