{"record":{"id":"58020b50dc8e57b2","repo":"dotnet/wpf","slug":"sr-format-sr-cyclicstylereferencedetected-this-58020b","errorCode":null,"errorMessage":"SR.Format(SR.CyclicStyleReferenceDetected, this)","messagePattern":"SR\\.Format\\(SR\\.CyclicStyleReferenceDetected, this\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs","lineNumber":625,"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(/* fe = */ this, /* fce = */ null);\n\n                    // Update the ContextMenu and ToolTips separately because they aren't in the tree\n                    ContextMenu contextMenu =","sourceCodeStart":607,"sourceCodeEnd":643,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Generated/FrameworkElement.cs#L607-L643","documentation":"FrameworkElement.UpdateStyleProperty detects re-entrancy: the StyleProperty update is invoked (via OnInitialized, InvalidateTreeDependentProperties, InvalidateOnTreeChange, OnResourcesChanged, or InvalidateStyleAndReferences) while IsStyleUpdateInProgress is already true, meaning the style resolution looped back onto the same element. WPF throws InvalidOperationException (CyclicStyleReferenceDetected) to stop infinite recursion.","triggerScenarios":"A style (or its BasedOn chain, setters, or triggers) triggers a style invalidation of the same element before the in-progress update completes — e.g. a Setter that sets Style, DynamicResource references that change Style during lookup, or overrides of OnInitialized/OnResourcesChanged that re-apply styles.","commonSituations":"A style Setter targeting StyleProperty itself; mutually referencing styles via BasedOn with DynamicResource; changing DefaultStyleKey/Style inside style callbacks; implicit styles that reference resources whose change invalidates the style again.","solutions":["Remove any Setter/trigger that sets StyleProperty within a style, and check BasedOn chains for self-referencing DynamicResource keys.","Defer style changes made in OnInitialized/OnResourcesChanged overrides with Dispatcher.BeginInvoke.","Keep DefaultStyleKeyProperty.OverrideMetadata values static per type; never change them in response to style invalidation.","Use the exception message (it includes the element) to identify the offending element and inspect its Style and resource keys."],"exampleFix":"// before\nvar s = new Style(typeof(Button), (Style)Application.Current.Resources[\"self\"]); // resources[\"self\"] BasedOn this style -> cycle\n\n// after\nvar s = new Style(typeof(Button), (Style)Application.Current.Resources[\"baseButtonStyle\"]); // distinct base style","handlingStrategy":"validation","validationCode":"if (element.IsStyleUpdateInProgress)\n    throw new InvalidOperationException(\"Style update already in progress; defer the style change.\");","typeGuard":"bool IsSafeForStyleUpdate(FrameworkElement e) => !e.IsStyleUpdateInProgress;","tryCatchPattern":"try\n{\n    element.Style = newStyle;\n}\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"cyclic\"))\n{\n    Log.Warn(\"Cyclic style reference detected.\", ex);\n}","preventionTips":["Never set StyleProperty from inside a style Setter, trigger, or style-invalidation callback.","Check BasedOn chains and DynamicResource keys for self-references.","Keep DefaultStyleKey metadata static per control type."],"tags":["wpf","cyclic-reference","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-21T21:30:21.729Z"}