diff MetroWpf/MetroWpf.Xaml/Transitions/StoryboardTransition.cs @ 24:a8b50a087544

Stocks and FxRates working, new menu introduced. Working nicely so far
author adminsh@apollo
date Tue, 20 Mar 2012 20:18:35 +0000
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MetroWpf/MetroWpf.Xaml/Transitions/StoryboardTransition.cs	Tue Mar 20 20:18:35 2012 +0000
@@ -0,0 +1,118 @@
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Media.Animation;
+
+namespace MetroWpf.Xaml.Transitions
+{
+    // Transition with storyboards for the old and new content presenters
+    [StyleTypedProperty(Property = "OldContentStyle", StyleTargetType = typeof(ContentPresenter))]
+    [StyleTypedProperty(Property = "NewContentStyle", StyleTargetType = typeof(ContentPresenter))]
+    public class StoryboardTransition : Transition
+    {
+        public Style OldContentStyle
+        {
+            get { return (Style)GetValue(OldContentStyleProperty); }
+            set { SetValue(OldContentStyleProperty, value); }
+        }
+
+        public static readonly DependencyProperty OldContentStyleProperty =
+            DependencyProperty.Register("OldContentStyle",
+                typeof(Style),
+                typeof(StoryboardTransition),
+                new UIPropertyMetadata(null));
+
+        public Storyboard OldContentStoryboard
+        {
+            get { return (Storyboard)GetValue(OldContentStoryboardProperty); }
+            set { SetValue(OldContentStoryboardProperty, value); }
+        }
+
+        public static readonly DependencyProperty OldContentStoryboardProperty =
+           DependencyProperty.Register("OldContentStoryboard",
+               typeof(Storyboard),
+               typeof(StoryboardTransition),
+               new UIPropertyMetadata(null));
+
+        public Style NewContentStyle
+        {
+            get { return (Style)GetValue(NewContentStyleProperty); }
+            set { SetValue(NewContentStyleProperty, value); }
+        }
+
+        public static readonly DependencyProperty NewContentStyleProperty =
+            DependencyProperty.Register("NewContentStyle",
+                typeof(Style),
+                typeof(StoryboardTransition),
+                new UIPropertyMetadata(null));
+
+        public Storyboard NewContentStoryboard
+        {
+            get { return (Storyboard)GetValue(NewContentStoryboardProperty); }
+            set { SetValue(NewContentStoryboardProperty, value); }
+        }
+
+        public static readonly DependencyProperty NewContentStoryboardProperty =
+            DependencyProperty.Register("NewContentStoryboard",
+                typeof(Storyboard),
+                typeof(StoryboardTransition),
+                new UIPropertyMetadata(null));
+
+        protected internal override void BeginTransition(TransitionPresenter transitionElement, ContentPresenter oldContent, ContentPresenter newContent)
+        {
+            Storyboard oldStoryboard = OldContentStoryboard;
+            Storyboard newStoryboard = NewContentStoryboard;
+
+            if (oldStoryboard != null || newStoryboard != null)
+            {
+                oldContent.Style = OldContentStyle;
+                newContent.Style = NewContentStyle;
+
+                // Flag to determine when both storyboards are done
+                bool done = oldStoryboard == null || newStoryboard == null;
+
+                if (oldStoryboard != null)
+                {
+                    oldStoryboard = oldStoryboard.Clone();
+                    oldContent.SetValue(OldContentStoryboardProperty, oldStoryboard);
+                    oldStoryboard.Completed += delegate
+                    {
+                        if (done)
+                            EndTransition(transitionElement, oldContent, newContent);
+                        done = true;
+                    };
+                    oldStoryboard.Begin(oldContent, true);
+                }
+
+                if (newStoryboard != null)
+                {
+                    newStoryboard = newStoryboard.Clone();
+                    newContent.SetValue(NewContentStoryboardProperty, newStoryboard);
+                    newStoryboard.Completed += delegate
+                    {
+                        if (done)
+                            EndTransition(transitionElement, oldContent, newContent);
+                        done = true;
+                    };
+                    newStoryboard.Begin(newContent, true);
+                }
+            }
+            else
+            {
+                EndTransition(transitionElement, oldContent, newContent);
+            }
+        }
+
+        protected override void OnTransitionEnded(TransitionPresenter transitionElement, ContentPresenter oldContent, ContentPresenter newContent)
+        {
+            Storyboard oldStoryboard = (Storyboard)oldContent.GetValue(OldContentStoryboardProperty);
+            if (oldStoryboard != null)
+                oldStoryboard.Stop(oldContent);
+            oldContent.ClearValue(ContentPresenter.StyleProperty);
+
+            Storyboard newStoryboard = (Storyboard)newContent.GetValue(NewContentStoryboardProperty);
+            if (newStoryboard != null)
+                newStoryboard.Stop(newContent);
+            newContent.ClearValue(ContentPresenter.StyleProperty);
+        }
+    }
+}