Update a LinearDoubleKeyFrame KeyTime value from code
I have some xaml like this:
<UserControl.Resources>
<Storyboard x:Name="sbLogo" x:Key="onLoadeducLogo" Completed="sbLogo_Completed">
<DoubleAnimationUsingKeyFrames Storyboard.TargetProperty="(UIElement.Opacity)" Storyboard.TargetName="image">
<LinearDoubleKeyFrame x:Name="pauseKeyFrame" KeyTime="0:0:2" Value="0"/>
<LinearDoubleKeyFrame x:Name="fadeInKeyFram" KeyTime="0:0:6" Value="1"/>
<LinearDoubleKeyFrame x:Name="fadeOutKeyFrame" KeyTime="0:0:12" Value="0"/>
</DoubleAnimationUsingKeyFrames>
</Storyboard>
</UserControl.Resources>
What I'd like to do is update the KeyTime
values of the LinearDoubleKeyFrame
elements from the UserControl code behind in C#.
I thought maybe I could do this by referencing those elements by their x:Name
but I'm not having much success. I also thought maybe I could bind the valu开发者_StackOverflowes to a field in the code behind, but no success there either.
Has anyone got any clues to push me in the right direction.
Thanks Phil
How have you tried to reference the LinearDoubleKeyFrame
objects in code?
I think you need to do something like:
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);
精彩评论