Click here to Skip to main content
Licence Ms-PL
First Posted 9 Jul 2010
Views 10,636
Downloads 223
Bookmarked 5 times

Thickness animation in Silverlight (Margin, Padding, BorderThickness)

By Nicolas Dorier | 9 Jul 2010
How to easily animate Thickness in Silverlight (Margin, Padding, BorderThickness).

1

2
1 vote, 11.1%
3
2 votes, 22.2%
4
6 votes, 66.7%
5
4.23/5 - 9 votes
μ 4.23, σa 1.30 [?]

Introduction

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">
    <Grid.Resources>
        <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>
        </Storyboard>
    </Grid.Resources>

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

    <StackPanel>
        <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>
    </StackPanel>
</Grid>

Conclusion

I have not found an easier solution! :)

License

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

Member

My goal as a developer, a community member, and enterpreneur is the same : making the minimal product that will help then getting myself out the way. My work is yours.

 
If you are interested for working with me, this way Smile | :)

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralThank you Pinmembercheburger0:19 8 Apr '11  
GeneralMy vote of 5 Pinmemberkcsass23:01 17 Mar '11  
GeneralMy vote of 5 Pinmemberkevin.keighran14:31 5 Aug '10  
GeneralMy vote of 3 PinmentorKunalChowdhury2:44 14 Jul '10  
GeneralRe: My vote of 3 PinmemberNicolas Dorier3:57 14 Jul '10  
GeneralThicknessAnimation PinmemberKodeur6:53 13 Jul '10  
GeneralRe: ThicknessAnimation PinmemberNicolas Dorier7:05 13 Jul '10  

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

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

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