//  ===========================================================================
        //  These methods are invoked when a Style/Template cache needs to be updated
        //  ===========================================================================

        #region UpdateCache

        //
        //  This method
        //  1. Updates the style cache for the given fe/fce
        //
        internal static void UpdateStyleCache(
            FrameworkElement        fe,
            FrameworkContentElement fce,
            Style                   oldStyle,
            Style                   newStyle,
            ref Style               styleCache)
        {
            Debug.Assert(fe != null || fce != null);

            if (newStyle != null)
            {
                // We have a new style.  Make sure it's targeting the right
                // type, and then seal it.

                DependencyObject d = fe;
                if (d == null)
                {
                    d = fce;
                }
                newStyle.CheckTargetType(d);
                newStyle.Seal();
            }

            styleCache = newStyle;

            // Do style property invalidations. Note that some of the invalidations may be callouts
            // that could turn around and query the style property on this node. Hence it is essential
            // to update the style cache before we do this operation.
            StyleHelper.DoStyleInvalidations(fe, fce, oldStyle, newStyle);

            // Now look for triggers that might want their EnterActions or ExitActions
            //  to run immediately.
            StyleHelper.ExecuteOnApplyEnterExitActions(fe, fce, newStyle, StyleDataField);
        }
        //
        //  This method
        //  1. Updates the theme style cache for the given fe/fce
        //
        internal static void UpdateThemeStyleCache(
            FrameworkElement        fe,
            FrameworkContentElement fce,
            Style                   oldThemeStyle,
            Style                   newThemeStyle,
            ref Style               themeStyleCache)
        {
            Debug.Assert(fe != null || fce != null);

            if (newThemeStyle != null)
            {
                DependencyObject d = fe;
                if (d == null)
                {
                    d = fce;
                }
                newThemeStyle.CheckTargetType(d);
                newThemeStyle.Seal();

#pragma warning disable 6503
                // Check if the theme style has the OverridesDefaultStyle  property set on the target tag or any of its
                // visual triggers. It is an error to specify the OverridesDefaultStyle  in your own ThemeStyle.
                if (StyleHelper.IsSetOnContainer(FrameworkElement.OverridesDefaultStyleProperty, ref newThemeStyle.ContainerDependents, true))
                {
                    throw new InvalidOperationException(SR.Get(SRID.CannotHaveOverridesDefaultStyleInThemeStyle));
                }
                // Check if the theme style has EventHandlers set on the target tag or int its setter collection.
                // We do not support EventHandlers in a ThemeStyle
                if (newThemeStyle.HasEventSetters)
                {
                    throw new InvalidOperationException(SR.Get(SRID.CannotHaveEventHandlersInThemeStyle));
                }
#pragma warning restore 6503
            }

            themeStyleCache = newThemeStyle;

            Style style = null;

            if (fe != null)
            {
                if(ShouldGetValueFromStyle ( FrameworkElement.DefaultStyleKeyProperty ) )
                {
                    style = fe.Style;
                }
            }
            else
            {
                if(ShouldGetValueFromStyle ( FrameworkContentElement.DefaultStyleKeyProperty ) )
                {
                    style = fce.Style;
                }
            }

            // Do theme style property invalidations. Note that some of the invalidations may be callouts
            // that could turn around and query the theme style property on this node. Hence it is essential
            // to update the theme style cache before we do this operation.
            StyleHelper.DoThemeStyleInvalidations(fe, fce, oldThemeStyle, newThemeStyle, style);

            // Now look for triggers that might want their EnterActions or ExitActions
            //  to run immediately.
            StyleHelper.ExecuteOnApplyEnterExitActions(fe, fce, newThemeStyle, ThemeStyleDataField);
        }