How to add animations as attached properties for a Window?? RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Windows;
    using System.Windows.Controls;
    using System.Windows.Media.Animation;
    public class MyClass : DependencyObject
     private static System.Windows.Media.Animation.Storyboard FadeIn;
        static MyClass()
          //Initializing the Fade in storyboard
          FadeIn = new System.Windows.Media.Animation.Storyboard();
          DoubleAnimation fadeinAnim = new DoubleAnimation();
          fadeinAnim.BeginTime = TimeSpan.FromMilliseconds(1);
          fadeinAnim.Duration = TimeSpan.FromMilliseconds(50);
          Storyboard.SetTargetProperty(fadeinAnim,new PropertyPath(Control.OpacityProperty));
    //dependencies properties
        public static DependencyProperty AnimationProperty = DependencyProperty.RegisterAttached("Animation", 
                                        typeof(bool), typeof(MyClass), 
                                        new FrameworkPropertyMetadata(false, SetTheAnimationCallback));
        public static bool GetAnimation(DependencyObject obj)
          return (bool)obj.GetValue(AnimationProperty );
        public static void SetAnimation(DependencyObject obj, bool value)
          obj.SetValue(AnimationProperty , value);
        private static void SetTheAnimationCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
          if ((bool)e.NewValue)
            //Setting the storyboard name
            Storyboard.SetTargetName(FadeIn, (d as Control).Name);
            //On load starting the animation.
            (d as Control).Loaded += OnLoaded;
    private static void OnLoaded(object sender,RoutedEventArgs e)
      //Load event occured , start animation.
    I made some animations whenever projects  MainWindow loads up.Now i have described it in xaml as resources and in event trigger for load event i started animation. Until now all was well. 

    Then , eventually i got to make same animations for all the child windows. Now what i thought that , why not make these animations as attached properties itself and add them to all windows !! it sounds great to me .I completed the class for that. 


     Code above is a cut down code for that. which can express the logic clearly. But it didnt happen any animation at the load of windows !!!!

    Well what can be the reasons ?? Am wondering ??? Plz help with this situation !!!


    Thanks in advance 





    Thursday, November 25, 2010 5:55 AM


  • Hi Avinash Rai,

    Based on the code you posted, I could not repro your problem. Could you kindly elaborate your issue? It would be appreciated if you could offer a simple and ready-to-run demo for us to debug with.

    Besides, I found only in your code that the fadeinAnim DoubleAnimation is not added into FadeIn StoryBoard. Are you missing this?


    Best regards,


    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
    • Marked as answer by Avinash Rai Wednesday, March 28, 2012 4:44 AM
    Monday, November 29, 2010 1:13 PM