{"record":{"id":"f5d678c3dbba3395","repo":"dotnet/wpf","slug":"sr-format-sr-compatibilitypreferencessealed-f5d678","errorCode":null,"errorMessage":"SR.Format(SR.CompatibilityPreferencesSealed, \"AextBoxDisplaysText\", \"FrameworkCompatibilityPreferences\")","messagePattern":"SR\\.Format\\(SR\\.CompatibilityPreferencesSealed, \"AextBoxDisplaysText\", \"FrameworkCompatibilityPreferences\"\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs","lineNumber":155,"sourceCode":"        /// As a second example, suppose the data item normalizes an integer by\n        /// capping its value to a maximum, say 100:\n        ///     public int Score { set { _score = Math.Min(value, 100); } }\n        /// And suppose the user types \"1004\".  Upon typing the 4, the binding converts\n        /// string \"1004\" to int 1004 and sends 1004 to the data item, which stores 100.\n        /// The round-trip continues, converting int 100 to string \"100\", which is\n        /// identical to the text before typing the 4.   The TextBox reaches a state\n        /// where its Text property has value \"100\", but it displays \"1004\".\n        /// </notes>\n        public static bool KeepTextBoxDisplaySynchronizedWithTextProperty\n        {\n            get { return _keepTextBoxDisplaySynchronizedWithTextProperty; }\n            set\n            {\n                lock (_lockObject)\n                {\n                    if (_isSealed)\n                    {\n                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, \"AextBoxDisplaysText\", \"FrameworkCompatibilityPreferences\"));\n                    }\n\n                    _keepTextBoxDisplaySynchronizedWithTextProperty = value;\n                }\n            }\n        }\n\n        internal static bool GetKeepTextBoxDisplaySynchronizedWithTextProperty()\n        {\n            Seal();\n\n            return KeepTextBoxDisplaySynchronizedWithTextProperty;\n        }\n\n        #endregion KeepTextBoxDisplaySynchronizedWithTextProperty\n\n        // There is a bug in the Windows desktop window manager which can cause\n        // incorrect z-order for windows when several conditions are all met:","sourceCodeStart":137,"sourceCodeEnd":173,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs#L137-L173","documentation":"Setting FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty after the preferences have been sealed throws InvalidOperationException. WPF seals these preferences automatically once the framework has started using them (e.g., when the first TextBox is created or the app is initialized), because compatibility switches must be fixed before the framework reads them. This guard ensures a deterministic single point of configuration at application startup.","triggerScenarios":"Assigning the static KeepTextBoxDisplaySynchronizedWithTextProperty property after FrameworkCompatibilityPreferences has been sealed - typically after Application startup, Dispatcher run, or any WPF control instantiation.","commonSituations":"Setting the preference in a window constructor, page Loaded handler, or after Application.Run instead of at the very start of Main() before any WPF type is touched.","solutions":["Move the assignment to the very beginning of Main(), before Application/Dispatcher/any WPF control is created","Set it in a static constructor or Module initializer that runs before WPF initializes","If runtime configuration is required, remove the setting entirely and rely on the default behavior"],"exampleFix":"// before\npublic MainWindow()\n{\n    InitializeComponent();\n    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; // throws\n}\n\n// after\nstatic void Main()\n{\n    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; // before any WPF use\n    var app = new App();\n    app.Run(new MainWindow());\n}","handlingStrategy":"validation","validationCode":"if (!FrameworkCompatibilityPreferences.IsSealed)\n    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;\n// Best: set in Main() before any WPF type is used.","typeGuard":"bool CanSetCompatibilityPrefs => !FrameworkCompatibilityPreferences.IsSealed;","tryCatchPattern":"try { FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = v; }\ncatch (InvalidOperationException ex) { log.Warn(\"Preference sealed; setting ignored\", ex); }","preventionTips":["Set compatibility preferences as the first statements of Main()","Never set them in window/page constructors or event handlers","Gate assignments with an IsSealed check"],"tags":["wpf","invalid-state-transition","compatibility-preferences"],"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"}