WPF C# Tutorial

  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
  xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  xmlns:d="http://schemas.microsoft.com/expression/interactivedesigner/2006"
  mc:Ignorable="d" Background="#FFFFFFFF" x:Name="DocumentRoot"
  x:Class="AnimationExamples.AnimationInCode" Width="640" Height="480">
  
    
  

  
    
      
    
  

  
  
    
  

  
    
  

  
  
  
    
      
        
        
        
      

    

  

//File:Window.xaml.cs
using System;
using System.IO;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Controls.Primitives;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace AnimationExamples
{
  public partial class AnimationInCode
  {
    public AnimationInCode()
    {
      this.InitializeComponent();
    }
    
    protected override void OnInitialized(EventArgs e)
    {
      base.OnInitialized(e);
      
      DoubleCollection tickMarks = new DoubleCollection();
      tickMarks.Add(0);
      tickMarks.Add(100);
      
      this.WidthControl.Ticks = tickMarks;
      this.WidthControl.TickPlacement = TickPlacement.BottomRight;
      this.WidthControl.AutoToolTipPlacement = AutoToolTipPlacement.TopLeft;
        this.WidthControl.AutoToolTipPrecision = 0;
      this.WidthControl.Value = this.WidthControl.Maximum;
      this.WidthControl.PreviewMouseUp += new MouseButtonEventHandler(WidthControl_MouseUp);
    }
    private void WidthControl_MouseUp(object sender, MouseButtonEventArgs e)
    {
      DoubleAnimation moveAnimation = new DoubleAnimation();
      moveAnimation.From = this.MyControl.Opacity;
      moveAnimation.To = this.WidthControl.Value / this.WidthControl.Maximum;
      moveAnimation.Duration = new Duration(TimeSpan.FromSeconds(.5));
      moveAnimation.DecelerationRatio = .5;
      
      MyControl.BeginAnimation(Shape.OpacityProperty, moveAnimation);
    }
  }
}