4

I was reading many similar q&a but I didn't get answer I'm searching for. So, I'm making "homework" in Microsoft Bled, I really like storyboards and I know how to trigger them with button click, but does anyone know how to start an animation in c# for example in if sentence.

Thanks for answers and time spend in advance!

5
  • You´re looking for this thread. Another way would be Storyboard sb = this.FindResource("Storyboard1") as Storyboard; if (sb != null){ BeginStoryboard(sb); } Commented Apr 9, 2016 at 14:23
  • Thanks, your solution works :) Can you post it as answer? Commented Apr 9, 2016 at 14:35
  • Glad it helped. Of course, I´ll post it as answer. :-) Commented Apr 9, 2016 at 14:37
  • Does this answer your question? Call a storyboard declared in xaml from c# Commented Oct 22, 2021 at 15:00
  • This is a dupe of stackoverflow.com/questions/3755651/… The accepted answer literally links to this other question. Commented Oct 22, 2021 at 15:00

3 Answers 3

4

You´re looking for this thread.


Another way would be:

Storyboard sb = this.FindResource("Storyboard1") as Storyboard;
if (sb != null){ BeginStoryboard(sb); }
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry because I'm writing you here in comment, but do you maybe know is it posible to use same animation reverse with code like that. For example close and open animation. Thanks :)
Not sure if this thread helps you, but you can create reversed storyboards inside blend with a few clicks. Take a look at this. Another alternative are VisualStates.
Thanks again, I did reverse animation
Glad I could help.
1
public static class AnimationHelper
{
    private static void AnimateOpacity(DependencyObject target, double from, double to)
    {
        var opacityAnimation = new DoubleAnimation
        {
            From = from,
            To = to,
            Duration = TimeSpan.FromMilliseconds(500)
        };

        Storyboard.SetTarget(opacityAnimation, target);
        Storyboard.SetTargetProperty(opacityAnimation, "Opacity");

        var storyboard = new Storyboard();
        storyboard.Children.Add(opacityAnimation);
        storyboard.Begin();
    }

    /// <summary>
    /// Fades in the given dependency object.
    /// </summary>
    /// <param name="target">The target dependency object to fade in.</param>
    public static void FadeIn(DependencyObject target)
    {
        AnimateOpacity(target, 0, 1);
    }
}

Comments

0

You can access storyboards from code-behind by giving it a name and referencing that name to use the Begin method.

<Canvas MouseLeftButtonDown="Handle_MouseDown"
  Background="Gray" Width="600" Height="500">
    <Canvas.Resources>
      <Storyboard x:Name="myStoryboard">
        <PointAnimation
          x:Name="myPointAnimation"
          Storyboard.TargetProperty="Center"
          Storyboard.TargetName="MyAnimatedEllipseGeometry"
          Duration="0:0:2"/>
      </Storyboard>
    </Canvas.Resources>

    <Path Fill="Blue">
      <Path.Data>
        <EllipseGeometry x:Name="MyAnimatedEllipseGeometry"
         Center="200,100" RadiusX="15" RadiusY="15" />
      </Path.Data>
    </Path>

</Canvas>

Code-behind:

private void Handle_MouseDown(object sender, MouseButtonEventArgs e)
{
    // Retrieve current mouse coordinates.
    double newX = e.GetPosition(null).X;
    double newY = e.GetPosition(null).Y;
    Point myPoint = new Point();
    myPoint.X = newX;
    myPoint.Y = newY;
    myPointAnimation.To = myPoint;
    myStoryboard.Begin();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.