{"record":{"id":"226537cfb6e32c33","repo":"dotnet/wpf","slug":"sr-format-sr-compatibilitypreferencessealed-nameof","errorCode":null,"errorMessage":"SR.Format(SR.CompatibilityPreferencesSealed, nameof(ShouldThrowOnCopyOrCutFailure), nameof(FrameworkCompatibilityPreferences))","messagePattern":"SR\\.Format\\(SR\\.CompatibilityPreferencesSealed, nameof\\(ShouldThrowOnCopyOrCutFailure\\), nameof\\(FrameworkCompatibilityPreferences\\)\\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs","lineNumber":376,"sourceCode":"        /// the application would take control of handling <see cref=\"System.Windows.Input.ApplicationCommands.Cut\"/>\n        /// and <see cref=\"System.Windows.Input.ApplicationCommands.Copy\"/> RoutedUICommands through a \n        /// <see cref=\"System.Windows.Input.CommandBinding\"/>, and apply that binding to all TextBoxBase\n        /// controls (<see cref=\"System.Windows.Controls.TextBox\"/> and <see cref=\"System.Windows.Controls.RichTextBox\"/>) \n        /// in the application. The application should ensure that it handles ExternalExeptions arising from Copy/Cut \n        /// operations in the CommandBinding's Executed handler. \n        /// </remarks>\n        public static bool ShouldThrowOnCopyOrCutFailure\n        {\n            get\n            {\n                return _shouldThrowOnCopyOrCutFailure;\n            }\n\n            set\n            {\n                if (_isSealed)\n                {\n                    throw new InvalidOperationException(\n                        SR.Format(SR.CompatibilityPreferencesSealed, \n                        nameof(ShouldThrowOnCopyOrCutFailure), \n                        nameof(FrameworkCompatibilityPreferences)));\n                }\n\n                _shouldThrowOnCopyOrCutFailure = value;\n            }\n        }\n\n        internal static bool GetShouldThrowOnCopyOrCutFailure()\n        {\n            Seal();\n            return ShouldThrowOnCopyOrCutFailure;\n        }\n\n        private static void SetShouldThrowOnCopyOrCutFailuresFromAppSettings(NameValueCollection appSettings)\n        {\n            // user can use config file to enable this behavior change","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs#L358-L394","documentation":"Setting FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure after the preferences are sealed throws InvalidOperationException. This switch decides whether TextBoxBase.Copy/Cut throw when clipboard operations fail; like the other compatibility switches it is sealed once WPF starts reading it, so late writes are rejected.","triggerScenarios":"Assigning the static ShouldThrowOnCopyOrCutFailure property after FrameworkCompatibilityPreferences.IsSealed becomes true (post WPF initialization / first clipboard-related control use).","commonSituations":"Configuring clipboard failure behavior in a textbox event handler or after Application.Run rather than during process startup.","solutions":["Set the property at the very start of Main() before any WPF object is created","Check IsSealed before assignment and log a warning if the setting cannot be applied","If per-operation control is needed, wrap Copy/Cut calls in try-catch at the call site instead of using the switch"],"exampleFix":"// before\nprivate void TextBox_CopyFailed(object s, EventArgs e)\n{\n    FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure = false; // throws\n}\n\n// after\nstatic void Main()\n{\n    FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure = false;\n    RunApp();\n}","handlingStrategy":"validation","validationCode":"if (!FrameworkCompatibilityPreferences.IsSealed)\n    FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure = false;","typeGuard":"bool PrefsWritable => !FrameworkCompatibilityPreferences.IsSealed;","tryCatchPattern":"try { FrameworkCompatibilityPreferences.ShouldThrowOnCopyOrCutFailure = v; }\ncatch (InvalidOperationException) { /* sealed; handle clipboard failures at call sites instead */ }","preventionTips":["Configure the switch during process startup only","Handle clipboard failures with try-catch around Copy/Cut calls instead","Do not toggle the flag in response to clipboard events"],"tags":["wpf","invalid-state-transition","clipboard"],"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"}