/// <summary>
        /// Retrieves or creates the merged style dictionary of an element.
        /// </summary>
        /// <param name="styledElement">A styled element.</param>
        /// <returns>The merged dictionary of the element.</returns>
        private static BaseMergedStyleDictionary GetMergedStyleDictionary(FrameworkElement styledElement)
        {
            Debug.Assert(styledElement != null, "styledElement cannot be null.");

            IList <FrameworkElement> ancestorsInclusive = styledElement.GetVisualAncestors().Append(styledElement).Reverse().ToList();

            IEnumerable <FrameworkElement> ancestorsTopToBottom = ancestorsInclusive;

            BaseMergedStyleDictionary initialDictionary = null;

            if (UseApplicationResources)
            {
                initialDictionary = new MergedStyleResourceDictionary(ApplicationExternalResourceDictionary ?? Application.Current.Resources);
            }
            else
            {
                FrameworkElement topLevelElement = ancestorsInclusive[0];
                initialDictionary = new MergedStyleResourceDictionary(ImplicitStyleManager.GetExternalResourceDictionary(topLevelElement) ?? topLevelElement.Resources);

                ancestorsTopToBottom = ancestorsInclusive.Skip(1);
            }

            BaseMergedStyleDictionary styleDictionary =
                ancestorsTopToBottom.Aggregate(
                    initialDictionary,
                    (dictionary, ancestor) => new MergedStyleResourceDictionary(ImplicitStyleManager.GetExternalResourceDictionary(ancestor) ?? ancestor.Resources)
            {
                Parent = dictionary
            });

            return(styleDictionary);
        }
        /// <summary>
        /// Retrieves or creates the merged style dictionary of an element.
        /// </summary>
        /// <param name="styledElement">A styled element.</param>
        /// <returns>The merged dictionary of the element.</returns>
        private static BaseMergedStyleDictionary GetMergedStyleDictionary(FrameworkElement styledElement)
        {
            Debug.Assert(styledElement != null, "styledElement cannot be null.");

            IList<FrameworkElement> ancestorsInclusive = styledElement.GetVisualAncestors().Append(styledElement).Reverse().ToList();

            IEnumerable<FrameworkElement> ancestorsTopToBottom = ancestorsInclusive;

            BaseMergedStyleDictionary initialDictionary = null;
            if (UseApplicationResources)
            {
                initialDictionary = new MergedStyleResourceDictionary(ApplicationExternalResourceDictionary ?? Application.Current.Resources);
            }
            else
            {
                FrameworkElement topLevelElement = ancestorsInclusive[0];
                initialDictionary = new MergedStyleResourceDictionary(ImplicitStyleManager.GetExternalResourceDictionary(topLevelElement) ?? topLevelElement.Resources);

                ancestorsTopToBottom = ancestorsInclusive.Skip(1);
            }

            BaseMergedStyleDictionary styleDictionary =
                ancestorsTopToBottom.Aggregate(
                    initialDictionary,
                    (dictionary, ancestor) => new MergedStyleResourceDictionary(ImplicitStyleManager.GetExternalResourceDictionary(ancestor) ?? ancestor.Resources) { Parent = dictionary });

            return styleDictionary;
        }