从代码更新LinearDoubleKeyFrame KeyTime值

我有一些像这样的xaml:

         

我想要做的是从C#后面的UserControl代码更新LinearDoubleKeyFrame元素的KeyTime值。

我想也许我可以通过x:Name引用这些元素来做到这一点,但我没有取得多大成功。 我也想过,也许我可以将值绑定到后面代码中的字段,但也没有成功。

有没有人有任何线索可以把我推向正确的方向。

谢谢菲尔

您是如何尝试在代码中引用LinearDoubleKeyFrame对象的?

我认为你需要做一些事情:

 var storyboard = (Storyboard)FindResource("onLoadeducLogo"); var animation = (DoubleAnimationUsingKeyFrames)storyboard.Children[0]; var keyframe1 = animation.KeyFrames[0]; keyframe1.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0,0,0,1)); // 1 second 
 Image creatureImage = new Image(); Storyboard fadeInFadeOut = new Storyboard(); DoubleAnimationUsingKeyFrames dbAnimation = new DoubleAnimationUsingKeyFrames(); dbAnimation.Duration = TimeSpan.FromSeconds(2); LinearDoubleKeyFrame lDKF1 = new LinearDoubleKeyFrame(); lDKF1.Value = 1; lDKF1.KeyTime = TimeSpan.FromSeconds(0); dbAnimation.KeyFrames.Add(lDKF1); // LinearDoubleKeyFrame lDKF2 = new LinearDoubleKeyFrame(); lDKF2.Value = 0.6; lDKF2.KeyTime = TimeSpan.FromSeconds(0.5); dbAnimation.KeyFrames.Add(lDKF2); // LinearDoubleKeyFrame lDKF3 = new LinearDoubleKeyFrame(); lDKF3.Value = 1; lDKF3.KeyTime = TimeSpan.FromSeconds(0.5); dbAnimation.KeyFrames.Add(lDKF3); // LinearDoubleKeyFrame lDKF4 = new LinearDoubleKeyFrame(); lDKF4.Value = 0; lDKF4.KeyTime = TimeSpan.FromSeconds(1); dbAnimation.KeyFrames.Add(lDKF4); Storyboard.SetTarget(dbAnimation, creatureImage); Storyboard.SetTargetProperty(dbAnimation, new PropertyPath(Image.OpacityProperty)); fadeInFadeOut.Children.Add(dbAnimation);