C# 如何使用C在WPF中触发动画#
我想在按下Crtl键时使用c#播放动画C# 如何使用C在WPF中触发动画#,c#,wpf,C#,Wpf,我想在按下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 }
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
}
}
假设
listBox1
是在XAML中声明的,并且您希望在其上应用淡入动画。您可以将不透明度从0切换为1,如下所示:
DoubleAnimation animation = new DoubleAnimation(0.0, 1.0,
new Duration(new TimeSpan(0,0,1)));
listBox1.BeginAnimation(ListBox.OpacityProperty, animation);
您也可以通过故事板实现这一点(但如果您只需使用双动画即可实现,则绝对没有用):
你会发布相关的XAML吗?好的,它可以工作,但是如果我想播放故事板动画而不是编码呢?你有在XAML中声明的故事板,还是想在代码隐藏中声明?
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();