{"record":{"id":"d2a9c0cbd5b4b23e","repo":"dotnet/wpf","slug":"sr-format-sr-compatibilitypreferencessealed","errorCode":null,"errorMessage":"SR.Format(SR.CompatibilityPreferencesSealed, \"AreInactiveSelectionHighlightBrushKeysSupported\", \"FrameworkCompatibilityPreferences\")","messagePattern":"SR\\.Format\\(SR\\.CompatibilityPreferencesSealed, \"AreInactiveSelectionHighlightBrushKeysSupported\", \"FrameworkCompatibilityPreferences\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs","lineNumber":81,"sourceCode":"\n#if NETFX && !NETCOREAPP\n        private static bool _areInactiveSelectionHighlightBrushKeysSupported = BinaryCompatibility.TargetsAtLeast_Desktop_V4_5 ? true : false;\n#elif NETCOREAPP\n        private static bool _areInactiveSelectionHighlightBrushKeysSupported = true;\n#else\n        private static bool _areInactiveSelectionHighlightBrushKeysSupported = true;\n#endif\n\n        public static bool AreInactiveSelectionHighlightBrushKeysSupported\n        {\n            get { return _areInactiveSelectionHighlightBrushKeysSupported; }\n            set\n            {\n                lock (_lockObject)\n                {\n                    if (_isSealed)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, \"AreInactiveSelectionHighlightBrushKeysSupported\", \"FrameworkCompatibilityPreferences\"));\n                    }\n\n                    _areInactiveSelectionHighlightBrushKeysSupported = value;\n                }\n            }\n        }\n\n        internal static bool GetAreInactiveSelectionHighlightBrushKeysSupported()\n        {\n            Seal();\n\n            return AreInactiveSelectionHighlightBrushKeysSupported;\n        }\n\n        #endregion AreInactiveSelectionHighlightBrushKeysSupported\n\n        #region KeepTextBoxDisplaySynchronizedWithTextProperty\n","sourceCodeStart":63,"sourceCodeEnd":99,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs#L63-L99","documentation":"A FrameworkCompatibilityPreferences static property (here AreInactiveSelectionHighlightBrushKeysSupported) was set after the preferences were sealed. WPF seals these app-wide compatibility flags when the first DependencyObject/window is created (or Seal is called), after which they are immutable to keep behavior consistent for the app lifetime.","triggerScenarios":"Setting a FrameworkCompatibilityPreferences property after the Application has started creating windows/DependencyObjects — e.g. in a Window constructor, Loaded handler, or any code running after startup initialization.","commonSituations":"Setting compatibility preferences in code-behind instead of App startup (before any window is shown); setting them in a library consumed by an app that already initialized WPF; unit tests where a prior test initialized WPF.","solutions":["Move the property assignment to the very start of App startup (e.g. App static constructor or before base Application initialization), before any DependencyObject is created.","Check FrameworkCompatibilityPreferences.GetAreInactiveSelectionHighlightBrushKeysSupportedDefault-style internal seal state or wrap in a sealed-check before setting when possible.","If a third-party library triggers the seal too early, set the preference before referencing that library, or use an Application-level workaround instead.","In tests, ensure each test process sets preferences before WPF initialization or isolate into separate processes."],"exampleFix":"// before (App.xaml.cs)\nprotected override void OnStartup(StartupEventArgs e)\n{\n    base.OnStartup(e);\n    FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = true; // throws\n}\n\n// after\nstatic App() // runs before any window/DependencyObject is created\n{\n    FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = true;\n}\nprotected override void OnStartup(StartupEventArgs e) => base.OnStartup(e);","handlingStrategy":"validation","validationCode":"// Set preferences before any WPF initialization, e.g. in [STAThread] Main:\nFrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = true;\n// then create Application/Window","typeGuard":null,"tryCatchPattern":"try { FrameworkCompatibilityPreferences.AreInactiveSelectionHighlightBrushKeysSupported = v; }\ncatch (InvalidOperationException) { /* sealed: preference must be set at app startup, restart required */ }","preventionTips":["Set all FrameworkCompatibilityPreferences before the first Window/DependencyObject is created — ideally in a static App constructor or Main.","Never set compatibility preferences from library code that may run after app startup.","In tests, configure preferences before WPF initialization or isolate per process."],"tags":["wpf","invalid-state-transition","sealed-object","app-lifecycle"],"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-22T01:17:13.364Z"}