Mega Code Archive

 
Categories / C# / WPF
 

Remove Animations with AnimationClock

<Window x:Class="WpfApplication1.Window1"   xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"   xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"   Title="" Height="300" Width="300">   <Window.Resources>     <Storyboard x:Key="Storyboard1">       <ParallelTimeline>         <DoubleAnimation x:Name="Animation1" Storyboard.TargetProperty="Width" From="140" To="50"           AutoReverse="True" RepeatBehavior="Forever" />         <DoubleAnimation Storyboard.TargetProperty="Opacity" To="0.5" AutoReverse="True" RepeatBehavior="Forever" />       </ParallelTimeline>     </Storyboard>   </Window.Resources>   <UniformGrid>     <Button x:Name="button3" Content="Method 3" Click="Button3_Click" Loaded="Button3_Loaded" />   </UniformGrid> </Window> //File:Window.xaml.cs using System; using System.Windows; using System.Windows.Controls; using System.Windows.Media.Animation; namespace WpfApplication1 {     public partial class Window1 : Window     {         private AnimationClock opacityClock;         private AnimationClock widthClock;         public Window1()         {             InitializeComponent();         }         private void Button3_Loaded(object sender, RoutedEventArgs e)         {             DoubleAnimation opacityAnimation = new DoubleAnimation(1d, 0.5d, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd);             opacityAnimation.RepeatBehavior = RepeatBehavior.Forever;             opacityAnimation.AutoReverse = true;             opacityClock = opacityAnimation.CreateClock();             button3.ApplyAnimationClock(Button.OpacityProperty, opacityClock);             DoubleAnimation widthAnimation = new DoubleAnimation(140d, 50d, TimeSpan.FromSeconds(1), FillBehavior.HoldEnd);             widthAnimation.RepeatBehavior = RepeatBehavior.Forever;             widthAnimation.AutoReverse = true;             widthClock = widthAnimation.CreateClock();             button3.ApplyAnimationClock(Button.WidthProperty, widthClock);         }         private void Button3_Click(object sender, RoutedEventArgs e)         {             opacityClock.Controller.Remove();             widthClock.Controller.Remove();         }             } }