{"record":{"id":"d833129321e558eb","repo":"dotnet/wpf","slug":"sr-format-sr-cyclicthemestylereferencedetected-this-d83312","errorCode":null,"errorMessage":"SR.Format(SR.CyclicThemeStyleReferenceDetected, this)","messagePattern":"SR\\.Format\\(SR\\.CyclicThemeStyleReferenceDetected, this\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs","lineNumber":679,"sourceCode":"                    if (toolTip != null)\n                    {\n                        FrameworkObject toolTipFO = new FrameworkObject(toolTip);\n                        if (toolTipFO.IsValid)\n                        {\n                            TreeWalkHelper.InvalidateOnResourcesChange(toolTipFO.FE, toolTipFO.FCE, ResourcesChangeInfo.ThemeChangeInfo);\n                        }\n                    }\n\n                    OnThemeChanged();\n                }\n                finally\n                {\n                    IsThemeStyleUpdateInProgress = false;\n                }\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.Format(SR.CyclicThemeStyleReferenceDetected, this));\n            }\n        }\n\n        // Called when the theme changes so resources not in the tree can be updated by subclasses\n        internal virtual void OnThemeChanged()\n        {\n        }\n\n        ///<summary>\n        ///     Initiate the processing for Loaded event broadcast starting at this node\n        /// </summary>\n        /// <remarks>\n        ///     This method is to allow firing Loaded event from a Helper class since the override is protected\n        /// </remarks>\n        internal void FireLoadedOnDescendentsInternal()\n        {\n            // This is to prevent duplicate Broadcasts for the Loaded event\n            if (LoadedPending == null)","sourceCodeStart":661,"sourceCodeEnd":697,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs#L661-L697","documentation":"FrameworkElement.UpdateThemeStyleProperty detects re-entrancy of the theme style update: it is invoked (via OnInitialized or OnResourcesChanged) while IsThemeStyleUpdateInProgress is already true, meaning the theme style resolution cycled back to the same element. WPF throws InvalidOperationException (CyclicThemeStyleReferenceDetected) to break the recursion.","triggerScenarios":"Theme style lookup for the element triggers OnResourcesChanged or re-initialization on the same element before the current theme style update finishes — e.g. resource references inside the theme style that invalidate the element's theme style again, or overrides that re-fetch theme style during the update.","commonSituations":"Custom control theme dictionaries with DynamicResource keys that change during style application; overriding OnThemeChanged/OnResourcesChanged to re-apply the theme style; resource dictionary churn while a theme style is being applied (e.g. theme switch handlers).","solutions":["Break the resource cycle: ensure the theme style's resource references do not point back to resources whose change re-invalidates the element's theme style.","Defer resource/style changes in OnThemeChanged/OnResourcesChanged overrides with Dispatcher.BeginInvoke.","Keep ThemeStyle/DefaultStyleKey metadata static; don't change theme-related keys during style application.","The exception message names the offending element — inspect its theme style and referenced dictionaries."],"exampleFix":"// before\nprotected override void OnThemeChanged()\n{\n    SetResourceReference(StyleProperty, ThemeKey); // re-invalidates theme style mid-update\n}\n\n// after\nprotected override void OnThemeChanged()\n{\n    Dispatcher.BeginInvoke(() => SetResourceReference(StyleProperty, ThemeKey));\n}","handlingStrategy":"validation","validationCode":"if (element.IsThemeStyleUpdateInProgress)\n    throw new InvalidOperationException(\"Theme style update already in progress; defer resource changes.\");","typeGuard":"bool IsSafeForThemeStyleUpdate(FrameworkElement e) => !e.IsThemeStyleUpdateInProgress;","tryCatchPattern":"try\n{\n    element.InvalidateProperty(ThemeStyleProperty);\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"cyclic\"))\n{\n    Log.Warn(\"Cyclic theme style reference detected.\", ex);\n}","preventionTips":["Avoid re-applying theme styles in OnThemeChanged/OnResourcesChanged overrides.","Ensure theme dictionary resources do not reference back to the element's own theme style.","Batch theme switches so resource dictionary changes don't invalidate styles mid-application."],"tags":["wpf","cyclic-reference","theme-style","invalid-operation"],"backgroundTag":"cyclic-style-reference-detected","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}