private void ColorAnimationInredRectangle() { // Create a Red SolidColorBrush SolidColorBrush redBrush = new SolidColorBrush(); redBrush.Color = Colors.Red;
// Create a Rectangle filled with Red brush Rectangle redRectangle = new Rectangle(); redRectangle.Width = 300; redRectangle.Height = 200; redRectangle.Fill = redBrush;
// Register the brush's name this.RegisterName("redBrush", redBrush);
// Animate brush with Green color ColorAnimation mouseEnterColorAnimation = new ColorAnimation(); mouseEnterColorAnimation.To = Colors.Green; mouseEnterColorAnimation.Duration = TimeSpan.FromSeconds(1); Storyboard.SetTargetName(mouseEnterColorAnimation, "redBrush"); Storyboard.SetTargetProperty( mouseEnterColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); Storyboard mouseEnterStoryboard = new Storyboard(); mouseEnterStoryboard.Children.Add(mouseEnterColorAnimation); redRectangle.MouseEnter += delegate(object sender, MouseEventArgs e) { mouseEnterStoryboard.Begin(this); };
// Animate color back to red when mouse leaves ColorAnimation mouseLeaveColorAnimation = new ColorAnimation(); mouseLeaveColorAnimation.To = Colors.Red; mouseLeaveColorAnimation.Duration = TimeSpan.FromSeconds(1); Storyboard.SetTargetName(mouseLeaveColorAnimation, "redBrush"); Storyboard.SetTargetProperty( mouseLeaveColorAnimation, new PropertyPath(SolidColorBrush.ColorProperty)); Storyboard mouseLeaveStoryboard = new Storyboard(); mouseLeaveStoryboard.Children.Add(mouseLeaveColorAnimation); redRectangle.MouseLeave += delegate(object sender, MouseEventArgs e) { mouseLeaveStoryboard.Begin(this); };
DoubleAnimation opacityAnimation = new DoubleAnimation(); opacityAnimation.To = 0.0; opacityAnimation.Duration = TimeSpan.FromSeconds(0.5); opacityAnimation.AutoReverse = true; Storyboard.SetTargetName(opacityAnimation, "redBrush"); Storyboard.SetTargetProperty( opacityAnimation, new PropertyPath(SolidColorBrush.OpacityProperty)); Storyboard mouseLeftButtonDownStoryboard = new Storyboard(); mouseLeftButtonDownStoryboard.Children.Add(opacityAnimation); redRectangle.MouseLeftButtonDown += delegate(object sender, MouseButtonEventArgs e) { mouseLeftButtonDownStoryboard.Begin(this); };
LayoutRoot.Children.Add(redRectangle); }
|