Home > Tips > Animating the Visibility property

Animating the Visibility property

This post is sort of a future reference for myself. I needed to animate the Visibility property as part of a larger Storyboard, and it wasn’t immediately apparent how to do it. It took a bit of effort to find the solution on Google, so I am posting the solution in hope that it will make someone else’s life easier as well.

If you have something like the following, for an object that is usually hidden:

<Setter Property="Visibility" Value="Visible" />

and you want to fade in the object instead of suddenly showing it, you will need to convert the Setter into an Animation action in a Storyboard. You are probably aware of the DoubleAnimation and BooleanAnimationUsingKeyFrames classes already. To animate the Visibility property, however – which is an enum –, the solution is not immediately apparent: you need to use ObjectAnimationUsingKeyFrames:

<Storyboard>
    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Visibility">
        <DiscreteObjectKeyFrame KeyTime="00:00:00">
            <DiscreteObjectKeyFrame.Value>
                <Visibility>Visible</Visibility>
            </DiscreteObjectKeyFrame.Value>
        </DiscreteObjectKeyFrame>
    </ObjectAnimationUsingKeyFrames>

    <DoubleAnimation Duration="00:00:01" Storyboard.TargetProperty="Opacity" To="1" />
</Storyboard>

There you go. The animation will start by making the object visible, and then animating it to 100% opaque. Hope this helps you!

Shout it

kick it on DotNetKicks.com

Share and Enjoy:
  • Digg
  • del.icio.us
  • DotNetKicks
  • DZone
  • Technorati
  • LinkedIn
  • Google Bookmarks
  • StumbleUpon
  • email
  • Print
Categories: Tips Tags: ,
  1. September 21st, 2009 at 15:55 | #1

    Looks great… but how would you use that in a setter? – I have a DataTrigger that I would normally set something visible, but I want it to animate visible instead (using the above code for example).

  2. September 22nd, 2009 at 23:20 | #2

    @Timothy Khouri

    If you want to fade something in, you would animate the Opacity property to reach the desired effect.

  1. August 31st, 2009 at 09:38 | #1