Monday, April 26, 2010

Animating the Visibility Property in Silverlight

Problem
If you define an animation in silverlight and try to animate the Visibility property (IE to hide or show an element on animation with other effects) it does not work. You will fine when you set the Visibility property it stays the entire animation and doesn't change.

Cause
If you are animating your properties, chances are you are using a double animation, which handles, you guessed it - double values. Visibility is not a double value. Its a specific enumeration value that requires in XAML the text name of the enum value.

Resolutions
In your animation simply add


<Storyboard x:Name="closeControl">
<ObjectAnimationUsingKeyFrames BeginTime="00:00:00" Storyboard.TargetName="controlToAnimate" Storyboard.TargetProperty="(UIElement.Visibility)">
<DiscreteObjectKeyFrame KeyTime="00:00:00">
<DiscreteObjectKeyFrame.Value>
<Visibility>Visible</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
<DiscreteObjectKeyFrame KeyTime="00:00:02">
<DiscreteObjectKeyFrame.Value>
<Visibility>Collapsed</Visibility>
</DiscreteObjectKeyFrame.Value>
</DiscreteObjectKeyFrame>
</ObjectAnimationUsingKeyFrames>
<DoubleAnimationUsingKeyFrames>
...
</DoubleAnimationUsingKeyFrames>
</Storyboard>




I included a placeholder there for a double animation to show this XAML simply goes on top of your existing DoubleAnimation.

2 comments:

Note: Only a member of this blog may post a comment.