使用故事板动画投影淡入/淡出

时间:2014-07-06 14:40:44

标签: c# wpf animation storyboard dropshadow

我希望在2秒内淡化DataGrid上的投影效果,在淡入动画完成后再次淡出2秒。

到目前为止我的代码:

DropShadowEffect dropShadowEffect = new DropShadowEffect();
dropShadowEffect.ShadowDepth = 0;
dropShadowEffect.Color = Colors.LightSeaGreen;
dropShadowEffect.Opacity = 0;
dropShadowEffect.BlurRadius = 20;
element.Effect = dropShadowEffect;

Storyboard storyboard1 = new Storyboard();
TimeSpan duration1 = TimeSpan.FromMilliseconds(2000);

DoubleAnimation animateOpacity1 = new DoubleAnimation() { From = 0, To = 1, Duration = new Duration(duration1) };
Storyboard.SetTargetName(animateOpacity1, element.Name);
Storyboard.SetTargetProperty(animateOpacity1, new PropertyPath(DropShadowEffect.OpacityProperty));

DoubleAnimation animateOpacity2 = new DoubleAnimation() { From = 1, To = 0, Duration = new Duration(duration1) };
Storyboard.SetTargetName(animateOpacity2, element.Name);
Storyboard.SetTargetProperty(animateOpacity2, new PropertyPath(DropShadowEffect.OpacityProperty));

storyboard1.Children.Add(animateOpacity1);
storyboard1.Children.Add(animateOpacity2);

storyboard1.Begin(element);

执行代码时,没有任何反应。

1 个答案:

答案 0 :(得分:2)

如果你只是想做DoubleAnimation,不需要使用StoryBoard来复杂它。此外,只需将 AutoReverse 属性设置为 true 的单个双动画即可实现此目的。

此外, 在dropShadowEffect对象上执行动画而不是元素对象

TimeSpan duration = TimeSpan.FromMilliseconds(2000);

DoubleAnimation animateOpacity = new DoubleAnimation() { From = 0, To = 1,
                                     Duration = duration, AutoReverse = true };

dropShadowEffect.BeginAnimation(DropShadowEffect.OpacityProperty,
                                animateOpacity);