【问题标题】:Animating drop shadow fade-in/fade-out with a storyboard使用故事板动画阴影淡入/淡出
【发布时间】:2014-07-06 14:40:44
【问题描述】:

我想在 DataGrid 上淡入 2 秒以上的阴影效果,在淡入动画完成后再次淡出 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);

执行代码后,什么也没有发生。

【问题讨论】:

    标签: c# wpf animation storyboard dropshadow


    【解决方案1】:

    如果您只是想做 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);
    

    【讨论】:

      猜你喜欢
      • 2012-08-14
      • 2017-08-03
      • 2014-01-20
      • 2012-12-18
      • 2019-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-14
      相关资源
      最近更新 更多