domingo, 28 de enero de 2007

Programmatic Fade In & Out sample

Animating the Opacity property is possible to achieve the fade in&out effect.

The source & bits can be downloaded from here.

The following is the C# code used,

#region Fade in
// Create a storyboard to contain the animations.
Storyboard storyboard = new Storyboard();
TimeSpan duration = new TimeSpan(0, 0, 1);

// Create a DoubleAnimation to fade the not selected option control
DoubleAnimation animation = new DoubleAnimation();

animation.From = 0.0;
animation.To = 1.0;
animation.Duration = new Duration(duration);
// Configure the animation to target de property Opacity
Storyboard.SetTargetName(animation, Img1.Name);
Storyboard.SetTargetProperty(animation, new PropertyPath(Control.OpacityProperty));
// Add the animation to the storyboard
storyboard.Children.Add(animation);

// Begin the storyboard
storyboard.Begin(this);

#endregion


The image used comes from here. The executed sample looks like this:


WPF Fade sample