I came across a different behaviour in Silverlight and WPF when an animated property values is held ("filled") after the animation has ended. The Remarks section in the documentation of the FillBehavior property says
The filling behavior can create the illusion that a property is unsettable at runtime if you are not careful about stopping unintended animations. Attempting to change the animated value coming from a filling animation in code will appear to have no effect until the filling animation is stopped.
However, in Silverlight this seems not to be true. In the small example below I animate the Opacity
of a filled Rectangle
to zero when the "Fade Out" button is clicked. Although the animation's FillBehavior
is set to HoldEnd
, resetting the Opacity to 1 in the "Reset" button click handler works and the Button reappears. If I do the same in a WPF application it behaves as expected, namely clicking the "Reset" button has no visual effect.
My question is, has anybody else observed this behavior? Is it perhaps a bug in Silverlight, or am I missing something?
I'm using Silverlight 5 and WPF 4.5 here.
<StackPanel HorizontalAlignment="Center">
<Rectangle Name="rect" Width="200" Height="100" Fill="DarkGreen"/>
<Button Content="Fade Out" Click="FadeOutButtonClick"/>
<Button Content="Reset" Click="ResetButtonClick"/>
</StackPanel>
-
private void FadeOutButtonClick(object sender, RoutedEventArgs e)
{
var animation = new DoubleAnimation
{
To = 0d,
Duration = TimeSpan.FromSeconds(1),
FillBehavior = FillBehavior.HoldEnd
};
Storyboard.SetTargetProperty(animation, new PropertyPath(UIElement.OpacityProperty));
Storyboard.SetTarget(animation, rect);
var storyboard = new Storyboard();
storyboard.Children.Add(animation);
storyboard.Begin();
}
private void ResetButtonClick(object sender, RoutedEventArgs e)
{
rect.Opacity = 1d;
}