简体   繁体   中英

How to create a WPF Storyboard in C# instead of XAML?

I have this Xaml code which I want to convert into c# :

<Storyboard x:Key="sb_hr">
    <ThicknessAnimationUsingKeyFrames Storyboard.TargetProperty="(FrameworkElement.Margin)" Storyboard.TargetName="ellipse">
        <EasingThicknessKeyFrame KeyTime="0" Value="56,48,0,0"/>
        <EasingThicknessKeyFrame KeyTime="0:0:1" Value="400,48,0,0"/>
    </ThicknessAnimationUsingKeyFrames>
</Storyboard>

How can I do that?

Try the below code.

EasingThicknessKeyFrame estf1 = new EasingThicknessKeyFrame();
estf1.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 1));
estf1.Value = new Thickness(56, 48, 0, 0);
EasingThicknessKeyFrame estf2 = new EasingThicknessKeyFrame();
estf2.KeyTime = KeyTime.FromTimeSpan(new TimeSpan(0, 0, 0, 1));
estf2.Value = new Thickness(400, 48, 0, 0);

ThicknessAnimationUsingKeyFrames th = new ThicknessAnimationUsingKeyFrames();
th.SetValue(Storyboard.TargetProperty, FrameworkElement.MarginProperty);
th.SetValue(Storyboard.TargetNameProperty, ellipse);
th.KeyFrames.Add(estf1);
th.KeyFrames.Add(estf2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM