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!

Laurent Resman
  • 133
  • 3
  • 13
  • You´re looking for this [thread](http://stackoverflow.com/questions/3755651/call-a-storyboard-declared-in-xaml-from-c-sharp). Another way would be `Storyboard sb = this.FindResource("Storyboard1") as Storyboard; if (sb != null){ BeginStoryboard(sb); }` – Nebelkraehe Apr 09 '16 at 14:23
  • Thanks, your solution works :) Can you post it as answer? – Laurent Resman Apr 09 '16 at 14:35
  • Glad it helped. Of course, I´ll post it as answer. :-) – Nebelkraehe Apr 09 '16 at 14:37
  • Does this answer your question? [Call a storyboard declared in xaml from c#](https://stackoverflow.com/questions/3755651/call-a-storyboard-declared-in-xaml-from-c-sharp) – StayOnTarget Oct 22 '21 at 15:00
  • This is a dupe of https://stackoverflow.com/questions/3755651/call-a-storyboard-declared-in-xaml-from-c-sharp The accepted answer literally links to this other question. – StayOnTarget Oct 22 '21 at 15:00

3 Answers3

4

You´re looking for this thread.


Another way would be:

Storyboard sb = this.FindResource("Storyboard1") as Storyboard;
if (sb != null){ BeginStoryboard(sb); }
Community
  • 1
  • 1
Nebelkraehe
  • 234
  • 2
  • 8
  • 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 :) – Laurent Resman Apr 09 '16 at 15:00
  • Not sure if this [thread](http://stackoverflow.com/questions/3389779/using-wpf-storyboard-forward-and-reverse) helps you, but you can create reversed storyboards inside blend with a few clicks. Take a look at [this](https://msdn.microsoft.com/en-us/library/cc294966.aspx). Another alternative are `VisualStates`. – Nebelkraehe Apr 09 '16 at 15:06
  • Thanks again, I did reverse animation – Laurent Resman Apr 09 '16 at 15:08
  • Glad I could help. – Nebelkraehe Apr 09 '16 at 15:11
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);
    }
}
Evgeny Gorbovoy
  • 765
  • 3
  • 20
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();
}
Glen Thomas
  • 10,190
  • 5
  • 33
  • 65