private static void OnRenderMouseOverChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ButtonChrome chrome = ((ButtonChrome)o);

            if (chrome.Animates)
            {
                if (!chrome.RenderPressed)
                {
                    if (((bool)e.NewValue))
                    {
                        if (chrome._localResources == null)
                        {
                            chrome._localResources = new LocalResources();
                            chrome.InvalidateVisual();
                        }

                        Duration duration = new Duration(TimeSpan.FromSeconds(0.3));

                        DoubleAnimation da = new DoubleAnimation(1, duration);

                        chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                        chrome.BackgroundOverlay.BeginAnimation(LinearGradientBrush.OpacityProperty, da);
                    }
                    else if (chrome._localResources == null)
                    {
                        chrome.InvalidateVisual();
                    }
                    else
                    {
                        if (chrome.RenderDefaulted)
                        {
                            // Since the mouse was over the button the opacity should be 1.0
                            // Create a repeating animation like:
                            //  \__/ \__/ \__/ \_...
                            // But if the user quickly mouses over a button, the opacity may not be 1 yet
                            // so the first keyframe brings the opacity to 1
                            double currentOpacity = chrome.BackgroundOverlay.Opacity;

                            // This is the time needed to complete the animation to 1:
                            double to1 = (1.0 - currentOpacity) * 0.5;

                            DoubleAnimationUsingKeyFrames daukf = new DoubleAnimationUsingKeyFrames();
                            daukf.KeyFrames.Add(new LinearDoubleKeyFrame(1.0, TimeSpan.FromSeconds(to1)));
                            daukf.KeyFrames.Add(new DiscreteDoubleKeyFrame(1.0, TimeSpan.FromSeconds(to1 + 0.25)));
                            daukf.KeyFrames.Add(new LinearDoubleKeyFrame(0.0, TimeSpan.FromSeconds(to1 + 1.5)));
                            daukf.KeyFrames.Add(new LinearDoubleKeyFrame(currentOpacity, TimeSpan.FromSeconds(2)));
                            daukf.RepeatBehavior = RepeatBehavior.Forever;
                            DoubleAnimationUsingKeyFrames.SetDesiredFrameRate(daukf, 10);
                            chrome.BackgroundOverlay.BeginAnimation(LinearGradientBrush.OpacityProperty, daukf);
                            chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, daukf);
                        }
                        else
                        {
                            Duration duration = new Duration(TimeSpan.FromSeconds(0.2));

                            DoubleAnimation da = new DoubleAnimation();
                            da.Duration = duration;

                            chrome.BackgroundOverlay.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                            chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                        }
                    }
                }
            }
            else
            {
                chrome._localResources = null;
                chrome.InvalidateVisual();
            }
        }
        private static void OnRenderPressedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ButtonChrome chrome = ((ButtonChrome)o);

            if (chrome.Animates)
            {
                if (((bool)e.NewValue))
                {
                    if (chrome._localResources == null)
                    {
                        chrome._localResources = new LocalResources();
                        chrome.InvalidateVisual();
                    }

                    Duration duration = new Duration(TimeSpan.FromSeconds(0.1));

                    DoubleAnimation da = new DoubleAnimation(1, duration);

                    chrome.BackgroundOverlay.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                    chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                    chrome.LeftDropShadowBrush.BeginAnimation(LinearGradientBrush.OpacityProperty, da);
                    chrome.TopDropShadowBrush.BeginAnimation(LinearGradientBrush.OpacityProperty, da);

                    da = new DoubleAnimation(0, duration);
                    chrome.InnerBorderPen.Brush.BeginAnimation(LinearGradientBrush.OpacityProperty, da);

                    ColorAnimation         ca  = new ColorAnimation(Color.FromRgb(0xC2, 0xE4, 0xF6), duration);
                    GradientStopCollection gsc = ((LinearGradientBrush)chrome.BackgroundOverlay).GradientStops;
                    gsc[0].BeginAnimation(GradientStop.ColorProperty, ca);
                    gsc[1].BeginAnimation(GradientStop.ColorProperty, ca);

                    ca = new ColorAnimation(Color.FromRgb(0xAB, 0xDA, 0xF3), duration);
                    gsc[2].BeginAnimation(GradientStop.ColorProperty, ca);

                    ca = new ColorAnimation(Color.FromRgb(0x90, 0xCB, 0xEB), duration);
                    gsc[3].BeginAnimation(GradientStop.ColorProperty, ca);

                    ca = new ColorAnimation(Color.FromRgb(0x2C, 0x62, 0x8B), duration);
                    chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.ColorProperty, ca);
                }
                else if (chrome._localResources == null)
                {
                    chrome.InvalidateVisual();
                }
                else
                {
                    bool     renderMouseOver = chrome.RenderMouseOver;
                    Duration duration        = new Duration(TimeSpan.FromSeconds(0.1));

                    DoubleAnimation da = new DoubleAnimation();
                    da.Duration = duration;
                    chrome.LeftDropShadowBrush.BeginAnimation(LinearGradientBrush.OpacityProperty, da);
                    chrome.TopDropShadowBrush.BeginAnimation(LinearGradientBrush.OpacityProperty, da);
                    chrome.InnerBorderPen.Brush.BeginAnimation(LinearGradientBrush.OpacityProperty, da);

                    if (!renderMouseOver)
                    {
                        chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                        chrome.BackgroundOverlay.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                    }

                    ColorAnimation ca = new ColorAnimation();
                    ca.Duration = duration;
                    chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.ColorProperty, ca);

                    GradientStopCollection gsc = ((LinearGradientBrush)chrome.BackgroundOverlay).GradientStops;
                    gsc[0].BeginAnimation(GradientStop.ColorProperty, ca);
                    gsc[1].BeginAnimation(GradientStop.ColorProperty, ca);
                    gsc[2].BeginAnimation(GradientStop.ColorProperty, ca);
                    gsc[3].BeginAnimation(GradientStop.ColorProperty, ca);
                }
            }
            else
            {
                chrome._localResources = null;
                chrome.InvalidateVisual();
            }
        }
        private static void OnRenderDefaultedChanged(DependencyObject o, DependencyPropertyChangedEventArgs e)
        {
            ButtonChrome chrome = ((ButtonChrome)o);

            if (chrome.Animates)
            {
                if (((bool)e.NewValue))
                {
                    if (chrome._localResources == null)
                    {
                        chrome._localResources = new LocalResources();
                        chrome.InvalidateVisual();
                    }

                    Duration duration = new Duration(TimeSpan.FromSeconds(0.3));

                    ColorAnimation         ca  = new ColorAnimation(Color.FromArgb(0xF9, 0x00, 0xCC, 0xFF), duration);
                    GradientStopCollection gsc = ((LinearGradientBrush)chrome.InnerBorderPen.Brush).GradientStops;
                    gsc[0].BeginAnimation(GradientStop.ColorProperty, ca);
                    gsc[1].BeginAnimation(GradientStop.ColorProperty, ca);

                    if (!chrome.RenderPressed)
                    {
                        // Create a repeating animation like:
                        //  __/ \__/ \__/ \__...
                        DoubleAnimationUsingKeyFrames daukf = new DoubleAnimationUsingKeyFrames();
                        daukf.KeyFrames.Add(new LinearDoubleKeyFrame(1.0, TimeSpan.FromSeconds(0.5)));
                        daukf.KeyFrames.Add(new DiscreteDoubleKeyFrame(1.0, TimeSpan.FromSeconds(0.75)));
                        daukf.KeyFrames.Add(new LinearDoubleKeyFrame(0.0, TimeSpan.FromSeconds(2.0)));
                        daukf.RepeatBehavior = RepeatBehavior.Forever;
                        DoubleAnimationUsingKeyFrames.SetDesiredFrameRate(daukf, 10);

                        chrome.BackgroundOverlay.BeginAnimation(LinearGradientBrush.OpacityProperty, daukf);
                        chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, daukf);
                    }
                }
                else if (chrome._localResources == null)
                {
                    if (!chrome.RenderPressed)
                    {
                        chrome.InvalidateVisual();
                    }
                }
                else
                {
                    Duration duration = new Duration(TimeSpan.FromSeconds(0.2));

                    if (!chrome.RenderPressed)
                    {
                        DoubleAnimation da = new DoubleAnimation();
                        da.Duration = duration;
                        chrome.BorderOverlayPen.Brush.BeginAnimation(SolidColorBrush.OpacityProperty, da);
                        chrome.BackgroundOverlay.BeginAnimation(LinearGradientBrush.OpacityProperty, da);
                    }

                    ColorAnimation ca = new ColorAnimation();
                    ca.Duration = duration;
                    GradientStopCollection gsc = ((LinearGradientBrush)chrome.InnerBorderPen.Brush).GradientStops;
                    gsc[0].BeginAnimation(GradientStop.ColorProperty, ca);
                    gsc[1].BeginAnimation(GradientStop.ColorProperty, ca);
                }
            }
            else
            {
                chrome._localResources = null;
                chrome.InvalidateVisual();
            }
        }