Creating Storyboard in code behind in WPF Creating Storyboard in code behind in WPF wpf wpf

Creating Storyboard in code behind in WPF


When working in code, you don't need Storyboard really, just animations for basic things, like you show in your question.I made a little sample to show how easy it works.

This is the complete code behind of the mainwindow:

namespace WpfCSharpSandbox{    public partial class MainWindow : Window    {        public MainWindow()        {            InitializeComponent();            WidenObject(150, TimeSpan.FromSeconds(1));        }        private void WidenObject(int newWidth, TimeSpan duration)        {            DoubleAnimation animation = new DoubleAnimation(newWidth, duration);            rctMovingObject.BeginAnimation(Rectangle.WidthProperty, animation);        }    }}

This is how the XAML looks like:

<Window x:Class="WpfCSharpSandbox.MainWindow"        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"        Title="Sandbox" Height="350" Width="525">    <Grid Background="#333333">        <Rectangle x:Name="rctMovingObject" Fill="LimeGreen" Width="50" Height="50"/>    </Grid></Window>

Put this in a WPF app and see how it works, experiment with it and try other animations/properties.


Adding djerry's comment sample code would look like this:

var anim = new DoubleAnimation {                                From = 1920,                                 To = 1,                                };wnd.BeginAnimation(Window.LeftProperty, anim); 

and you would have to have this code in window loaded event handler. Hope this helps.


The question's example code was about animating the Window.Left property and I was looking for exact that case, but the given answer does work for an one-time use-case only.
Specifically: If the animation has been performed and the Window is then moved manually via drag&drop, the same animation procedure will not work again as desired. The animation will always use the end-coordinates of the recent animation run.
So if you moved the window, it will jump back before starting the new animation:

https://imgur.com/a/hxRCqm7

To solve that issue, it is required to remove any AnimationClock from the animated property after the animation is completed.

That is done by using ApplyAnimationClock or BeginAnimation with null as the second parameter:

public partial class MainWindow : Window{    // [...]    private void ButtonMove_Click(object sender, RoutedEventArgs e)    {        AnimateWindowLeft(500, TimeSpan.FromSeconds(1));    }    private void AnimateWindowLeft(double newLeft, TimeSpan duration)    {        DoubleAnimation animation = new DoubleAnimation(newLeft, duration);        myWindow.Completed += AnimateLeft_Completed;        myWindow.BeginAnimation(Window.LeftProperty, animation);    }    private void AnimateLeft_Completed(object sender, EventArgs e)    {        myWindow.BeginAnimation(Window.LeftProperty, null);        // or        // myWindow.ApplyAnimationClock(Window.LeftProperty, null);    }}

XAML:

<Window x:Class="WpfAppAnimatedWindowMove.MainWindow"        // [...]        Name="myWindow">

Result:
https://imgur.com/a/OZEsP6t

See also Remarks section of Microsoft Docs - HandoffBehavior Enum