Mega Code Archive

 
Categories / C# / WPF
 

Animation In Code

<Grid xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"    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">   <Grid.Resources>     <Storyboard x:Key="OnLoaded"/>   </Grid.Resources>   <Grid.Triggers>     <EventTrigger RoutedEvent="FrameworkElement.Loaded">       <BeginStoryboard x:Name="OnLoaded_BeginStoryboard" Storyboard="{DynamicResource OnLoaded}"/>     </EventTrigger>   </Grid.Triggers>      <Grid.ColumnDefinitions>     <ColumnDefinition/>   </Grid.ColumnDefinitions>   <Grid.RowDefinitions>     <RowDefinition/>   </Grid.RowDefinitions>   <Slider d:LayoutOverrides="Width, Height" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,212,87" Width="105" Height="33" x:Name="WidthControl" Maximum="100" Minimum="0"/>   <Label d:LayoutOverrides="Width, Height" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="208,0,0,97.893333333333" Width="100" Height="23.2766666666667" x:Name="ContentLabel" Content="Circle Opacity:" RenderTransformOrigin="0.5,0.5" TabIndex="4"/>   <Ellipse Stroke="{x:Null}" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="228,163,233,138" Width="Auto" Height="Auto" x:Name="MyControl">     <Ellipse.Fill>       <RadialGradientBrush>         <GradientStop Color="#FFFFFFFF" Offset="0"/>         <GradientStop Color="#FF87001C" Offset="0.73735921399473"/>         <GradientStop Color="#FF4C000F" Offset="1"/>       </RadialGradientBrush>     </Ellipse.Fill>   </Ellipse> </Grid> //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);     }   } }