Reputation: 540
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
}
}
Upvotes: 0
Views: 188
Reputation: 81303
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();
Upvotes: 2