Click here to Skip to main content
11,935,031 members (51,555 online)
Click here to Skip to main content
Add your own
alternative version


6 bookmarked

Thickness animation in Silverlight (Margin, Padding, BorderThickness)

, 9 Jul 2010 Ms-PL
Rate this:
Please Sign up or sign in to vote.
How to easily animate Thickness in Silverlight (Margin, Padding, BorderThickness).


In Silverlight, there is no ThicknessAnimation as we can use in WPF. So like me, lots of developers would like to be able to animate the Margin, the Padding, or the BorderThickness of the Border.

Good news! I have a clean solution based on the Wrapper pattern.

How does it work?

Everything is done with the ThicknessWrapper class:

  • PropertyName is the name of the property to animate.
  • Side is the side to animate; you can specify multiple values since Side is a flag enumeration.
  • Target is the target object to animate.
  • Value is the size of the side of the Thickness to animate.

The code is not very complicated; every time one of these properties change, I update the margin of the Target.

private void UpdateMargin()
    if(Target != null)
        var thicknessProperty = Target.GetType().GetProperty(PropertyName);
        var currentThickness = (Thickness)thicknessProperty.GetValue(Target, null);
        var nextThickness = new Thickness(
            CalculateThickness(Side.Left,  currentThickness.Left),
            CalculateThickness(Side.Top,  currentThickness.Top),
            CalculateThickness(Side.Right, currentThickness.Right),
            CalculateThickness(Side.Bottom, currentThickness.Bottom)
        thicknessProperty.SetValue(Target, nextThickness, null);

private double CalculateThickness(ThicknessAnimation.Side sideToCalculate, 
               double currentValue)
    return (Side & sideToCalculate) == sideToCalculate ? Value : currentValue;

Now you just have to use the wrapper in your XAML code, and animate it. As an example, I play an animation on the BorderThickness and on the Margin of my border.

<Grid x:Name="LayoutRoot" Background="White">
        <Storyboard x:Key="animation">
            <DoubleAnimation Storyboard.TargetName="rectTopMargin" 
               Storyboard.TargetProperty="Value" From="0" 
               To="100" Duration="00:00:1"></DoubleAnimation>
            <DoubleAnimation Storyboard.TargetName="rectStrokeThickness" 
               Storyboard.TargetProperty="Value" From="0" 
               To="20" Duration="00:00:1"></DoubleAnimation>

    <local:ThicknessWrapper x:Name="rectTopMargin" 
       Target="{Binding ElementName=rect}" Side="Top" 
    <local:ThicknessWrapper x:Name="rectStrokeThickness" 
       Target="{Binding ElementName=rect}" Side="Left, Right" 

        <Button Content="Click to animate" Click="Button_Click"></Button>
        <Border x:Name="rect" HorizontalAlignment="Left" 
            BorderBrush="Black" VerticalAlignment="Top" 
            Background="Green" Height="50" Width="50"></Border>


I have not found an easier solution! Smile | :)


This article, along with any associated source code and files, is licensed under The Microsoft Public License (Ms-PL)


About the Author

Nicolas Dorier
Software Developer Freelance
France France
I am currently the CTO of Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.

I also developed a tool to make IaaS on Azure more easy to use IaaS Management Studio.

If you want to contact me, go this way Smile | :)

You may also be interested in...

Comments and Discussions

QuestionDoesn't seem to work in WinRT Pin
Member 1146972021-Feb-15 17:33
memberMember 1146972021-Feb-15 17:33 
AnswerRe: Doesn't seem to work in WinRT Pin
Nicolas Dorier22-Feb-15 1:12
professionalNicolas Dorier22-Feb-15 1:12 
GeneralRe: Doesn't seem to work in WinRT Pin
Member 114697207-Mar-15 13:45
memberMember 114697207-Mar-15 13:45 
QuestionThis is awesome! Pin
piXy^23-May-12 5:30
memberpiXy^23-May-12 5:30 
GeneralThank you Pin
cheburger8-Apr-11 0:19
membercheburger8-Apr-11 0:19 
GeneralMy vote of 5 Pin
kcsass17-Mar-11 23:01
memberkcsass17-Mar-11 23:01 
GeneralMy vote of 5 Pin
kevin.keighran5-Aug-10 14:31
memberkevin.keighran5-Aug-10 14:31 
GeneralMy vote of 3 Pin
KunalChowdhury14-Jul-10 2:44
mentorKunalChowdhury14-Jul-10 2:44 
GeneralRe: My vote of 3 Pin
Nicolas Dorier14-Jul-10 3:57
memberNicolas Dorier14-Jul-10 3:57 
GeneralThicknessAnimation Pin
Kodeur13-Jul-10 6:53
memberKodeur13-Jul-10 6:53 
GeneralRe: ThicknessAnimation Pin
Nicolas Dorier13-Jul-10 7:05
memberNicolas Dorier13-Jul-10 7:05 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.151126.1 | Last Updated 9 Jul 2010
Article Copyright 2010 by Nicolas Dorier
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid