The following code is working fine.
<Window.Triggers>
<EventTrigger RoutedEvent="Window.Loaded">
<BeginStoryboard>
<Storyboard>
<DoubleAnimation Duration="0:0:.8" Storyboard.TargetProperty="Left" From="1920" To="0" AccelerationRatio=".1"/>
</Storyboard>
</BeginStoryboard>
</EventTrigger>
</Window.Triggers>
But in this From
and To
values are static. I need to pass the values dynamically based system resolution. So i need it to be created in code behind. Is it possible to do ?
How to convert it to codebehind?
Adding djerry's comment sample code would look like this:
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
orBeginAnimation
withnull
as the second parameter:XAML:
Result:
https://imgur.com/a/OZEsP6t
See also Remarks section of Microsoft Docs - HandoffBehavior Enum
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:
This is how the XAML looks like:
Put this in a WPF app and see how it works, experiment with it and try other animations/properties.