{"record":{"id":"125c97723e2db590","repo":"dotnet/wpf","slug":"sr-cyclicstylereferencedetected","errorCode":null,"errorMessage":"SR.CyclicStyleReferenceDetected","messagePattern":"SR\\.CyclicStyleReferenceDetected","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs","lineNumber":707,"sourceCode":"                                    {\n                                        if (!HasStyleInvalidated)\n                                        {\n                                            if (IsStyleUpdateInProgress == false)\n                                            {\n                                                IsStyleUpdateInProgress = true;\n                                                try\n                                                {\n                                                    InvalidateProperty(StyleProperty);\n                                                    HasStyleInvalidated = true;\n                                                }\n                                                finally\n                                                {\n                                                    IsStyleUpdateInProgress = false;\n                                                }\n                                            }\n                                            else\n                                            {\n                                                throw new InvalidOperationException(SR.Format(SR.CyclicStyleReferenceDetected, this));\n                                            }\n                                        }\n                                    }\n\n                                    /// <summary>\n                                    ///     This method causes the ThemeStyleProperty to be re-evaluated\n                                    /// </summary>\n                                    internal void UpdateThemeStyleProperty()\n                                    {\n                                        if (IsThemeStyleUpdateInProgress == false)\n                                        {\n                                            IsThemeStyleUpdateInProgress = true;\n                                            try\n                                            {\n                                                StyleHelper.GetThemeStyle([[instance.ThisString]]);\n\n                                                // Update the ContextMenu and ToolTips separately because they aren't in the tree\n                                                ContextMenu contextMenu =","sourceCodeStart":689,"sourceCodeEnd":725,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WpfGfx/codegen/mcg/generators/FrameworkElementTemplate.cs#L689-L725","documentation":"While a style is being applied, FrameworkElement checks that the incoming style does not create a reference cycle (e.g. a style whose BasedOn chain or resource lookup loops back to the element itself). If a cycle is detected during the style update, it throws InvalidOperationException(SR.CyclicStyleReferenceDetected) with the element in the message.","triggerScenarios":"Setting FrameworkElement.Style to a Style that (transitively, via BasedOn or DynamicResource references) references the same element/style — detected because IsStyleUpdateInProgress is already true when the update re-enters.","commonSituations":"Style with BasedOn={StaticResource self-referencing key}; a control template setting its own Style; resource dictionaries with mutually referencing styles where the loop resolves back to the same element.","solutions":["Break the BasedOn/resource cycle: make the style chain acyclic and remove self-referencing resource keys","Use {DynamicResource} indirection carefully so the target style is not itself re-entered during update","Rename conflicting resource keys so the lookup does not resolve back to the same style/element"],"exampleFix":"<!-- before: self-referencing BasedOn -->\n<Style x:Key=\"s\" TargetType=\"Button\" BasedOn=\"{StaticResource s}\"/>\n<!-- after: base style separate from derived -->\n<Style x:Key=\"base\" TargetType=\"Button\"/>\n<Style x:Key=\"s\" TargetType=\"Button\" BasedOn=\"{StaticResource base}\"/>","handlingStrategy":"validation","validationCode":"// Walk the BasedOn chain and reject cycles before assigning\nStyle s = candidate; var seen = new HashSet<Style>();\nwhile (s != null) { if (!seen.Add(s)) throw new InvalidOperationException(\"Cyclic style reference detected.\"); s = s.BasedOn; }","typeGuard":"bool IsAcyclicStyle(Style style) { var seen = new HashSet<Style>(); for (var s = style; s != null; s = s.BasedOn) if (!seen.Add(s)) return false; return true; }","tryCatchPattern":"try { el.Style = candidate; } catch (InvalidOperationException ex) when (ex.Message.Contains(\"cyclic\")) { /* inspect and repair the BasedOn/resource chain */ }","preventionTips":["Keep BasedOn chains acyclic; never base a style on its own key","Avoid templates that set their own Style property","Check resource-dictionary merges for mutually referencing style keys"],"tags":["wpf","styles","resources","cycle"],"backgroundTag":"invalid-state-transition","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}