0

I want to play animation using c# when i press Crtl

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
    }
}
1
  • Would you post the relevant XAML? Commented Jan 29, 2014 at 19:29

1 Answer 1

2

Assuming listBox1 is declared in XAML and you want to apply Fade-In animation on it. You can toggle opacity from 0 to 1 like this:

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):

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();
Sign up to request clarification or add additional context in comments.

2 Comments

Ok its working but what if i want to play storyboard animation instead of coding?
Do you have storyboard declared in xaml or want to declare that in code behind as well?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.