Skip to content
Bruno Sonnino
Menu
  • Home
  • About
Menu

Animating transitions in WPF/Silverlight–Part I–Code Behind

Posted on 11 January 2012

When I saw a question from an user in the MSDN forums about animating an user control to give the exit effect while clicking a button, I thought this would be an excellent opportunity to create a series of posts here in the blog, showing many ways to do it.

Although I will put the alternatives here, some can be not so good: this post, putting animation on the code behind, although easy to implement, has these disadvantages:

  • The animation code is put on the code behind, a technique I don’t like. I don’t think that the MVVM pattern should have zero code behind, but I don’t like to put code behind, unless is indispensable and strictly related to the view.
  • It’s not design friendly – if the designer wants to change the animation, the code should be changed.
  • It’s not portable – if you want to create the same animation on other places, the code must be copied or refactored to be accessed from other places.
  • It’s not easy to manipulate – everything is on the code. Any changes, like the duration of the animation must be changed in code.

We have the following problem: we have a control in the window and we want to hide it when the button is clicked with an animation, like we see below:




To do that, we should create a transformation for the control, of type RenderTransform and animate it. We can do everything in code this way: at the button click, we create the transform, add it to the control and animate it:

private void Button_Click(object sender, RoutedEventArgs e)
{
// create the transformation and add it to the control
    var translate = new TranslateTransform(0, 0);
    gridLogin.RenderTransform = translate;
// create the animation and start it    
    var da = new DoubleAnimation(0, -ActualWidth, new Duration(TimeSpan.FromMilliseconds(1000)));
    translate.BeginAnimation(TranslateTransform.XProperty, da);
}
C#

This code has many shortcomings:

  • It can only be applied to our control. To add it to another control, we must copy the code.
  • The animation duration is 1 second – the duration is fixed.
  • It only animates from right to left.

In this first phase, we can refactor the code and leave it more generic:

public enum AnimationType
{
    Right,
    Left,
    Up,
    Down
}

private void Button_Click(object sender, RoutedEventArgs e)
{
    AnimateControl(gridLogin, TimeSpan.FromMilliseconds(1000), AnimationType.Left);
}

private void AnimateControl(UIElement control, TimeSpan duration, AnimationType type)
{
    double xEnd = 0;
    double yEnd = 0;
    if (type == AnimationType.Left)
        xEnd = -ActualWidth;
    else if (tipo == AnimationType.Right)
        xEnd = ActualWidth;
    else if (tipo == AnimationType.Up)
        yEnd = -ActualHeight;
    else if (tipo == AnimationType.Down)
        yEnd = ActualHeight;
    // create the transformation and add it to the control
    var translate = new TranslateTransform(0, 0);
    control.RenderTransform = translate;
    // create the animation and start it
    if (tipo == AnimationType.Left || tipo == AnimationType.Right)
    {
        var da = new DoubleAnimation(0, xEnd, new Duration(duration));
        translate.BeginAnimation(TranslateTransform.XProperty, da);
    }
    else
    {
        var da = new DoubleAnimation(0, yEnd, new Duration(duration));
        translate.BeginAnimation(TranslateTransform.YProperty, da);
    }
}
C#

Although the code is larger, it is more generic and allows to be reutilized. We can use any control, set the duration and the animation direction. It still isn’t ideal, but it’s better than last code, non reutilizable. On the next posts, we will see other ways to do it. See you then!

2 thoughts on “Animating transitions in WPF/Silverlight–Part I–Code Behind”

  1. Pingback: Animating transitions in WPF/Silverlight–Part III–Visual States – Bruno Sonnino
  2. Pingback: Animating transitions in WPF/Silverlight–Part II–Using Components – Bruno Sonnino

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

  • May 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • June 2024
  • November 2023
  • October 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • June 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • July 2021
  • June 2021
  • May 2021
  • April 2021
  • March 2021
  • February 2021
  • January 2021
  • December 2020
  • October 2020
  • September 2020
  • April 2020
  • March 2020
  • January 2020
  • November 2019
  • September 2019
  • August 2019
  • July 2019
  • June 2019
  • April 2019
  • March 2019
  • February 2019
  • January 2019
  • December 2018
  • November 2018
  • October 2018
  • September 2018
  • August 2018
  • July 2018
  • June 2018
  • May 2018
  • November 2017
  • October 2017
  • September 2017
  • August 2017
  • June 2017
  • May 2017
  • March 2017
  • February 2017
  • January 2017
  • December 2016
  • November 2016
  • October 2016
  • September 2016
  • August 2016
  • July 2016
  • June 2016
  • May 2016
  • April 2016
  • March 2016
  • February 2016
  • October 2015
  • August 2013
  • May 2013
  • February 2012
  • January 2012
  • April 2011
  • March 2011
  • December 2010
  • November 2009
  • June 2009
  • April 2009
  • March 2009
  • February 2009
  • January 2009
  • December 2008
  • November 2008
  • October 2008
  • July 2008
  • March 2008
  • February 2008
  • January 2008
  • December 2007
  • November 2007
  • October 2007
  • September 2007
  • August 2007
  • July 2007
  • Development
  • English
  • Português
  • Uncategorized
  • Windows

.NET AI Algorithms asp.NET Backup C# Debugging Delphi Dependency Injection Desktop Bridge Desktop icons Entity Framework JSON Linq Mef Minimal API MVVM NTFS Open Source OpenXML OzCode PowerShell Sensors Silverlight Source Code Generators sql server Surface Dial Testing Tools TypeScript UI Unit Testing UWP Visual Studio VS Code WCF WebView2 WinAppSDK Windows Windows 10 Windows Forms Windows Phone WPF XAML Zip

  • Entries RSS
  • Comments RSS
©2025 Bruno Sonnino | Design: Newspaperly WordPress Theme
Menu
  • Home
  • About