简体   繁体   English

如何使用C#在WPF中触发动画

[英]How to Trigger animation in WPF using C#

I want to play animation using c# when i press Crtl 当我按下Crtl时,我想用c#播放动画

private void rtb_PreviewKeyUp(object sender, System.Windows.Input.KeyEventArgs e)
{
    if (e.Key == Key.LeftCtrl || e.Key == Key.RightCtrl)
    {
        //lstBox1.Opacity = 1;
        //here i want to play fadeIn animation
    }
}

Assuming listBox1 is declared in XAML and you want to apply Fade-In animation on it. 假设listBox1在XAML中声明,并且您想在其上应用Fade-In动画 You can toggle opacity from 0 to 1 like this: 您可以将不透明度从0切换为1,如下所示:

DoubleAnimation animation = new DoubleAnimation(0.0, 1.0,
                                             new Duration(new TimeSpan(0,0,1)));
listBox1.BeginAnimation(ListBox.OpacityProperty, animation);

You can achieve that with Storyboard as well (but definitely no use when you can achieve that simply using double animation): 你也可以通过Storyboard实现这一点(但是当你只使用双动画时就可以实现这一点):

DoubleAnimation animation = new DoubleAnimation(0.0, 1.0,
                               new Duration(new TimeSpan(0, 0, 2)));
Storyboard storyBoard = new Storyboard();
storyBoard.Children.Add(animation);
Storyboard.SetTarget(animation, listBox);
Storyboard.SetTargetProperty(animation, new PropertyPath("Opacity"));
storyBoard.Begin();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM