dotnet/wpf · error · InvalidOperationException

SR.Format(SR.CompatibilityPreferencesSealed…

Error message

SR.Format(SR.CompatibilityPreferencesSealed, "AextBoxDisplaysText", "FrameworkCompatibilityPreferences")

What it means

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.

Solutions

  1. Move the assignment to the very beginning of Main(), before Application/Dispatcher/any WPF control is created
  2. Set it in a static constructor or Module initializer that runs before WPF initializes
  3. If runtime configuration is required, remove the setting entirely and rely on the default behavior

Example fix

// before
public MainWindow()
{
    InitializeComponent();
    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; // throws
}

// after
static void Main()
{
    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false; // before any WPF use
    var app = new App();
    app.Run(new MainWindow());
}
Defensive patterns

Strategy: validation

Validate before calling

if (!FrameworkCompatibilityPreferences.IsSealed)
    FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = false;
// Best: set in Main() before any WPF type is used.

Type guard

bool CanSetCompatibilityPrefs => !FrameworkCompatibilityPreferences.IsSealed;

Try / catch

try { FrameworkCompatibilityPreferences.KeepTextBoxDisplaySynchronizedWithTextProperty = v; }
catch (InvalidOperationException ex) { log.Warn("Preference sealed; setting ignored", ex); }

Prevention

When it happens

Trigger: Assigning the static KeepTextBoxDisplaySynchronizedWithTextProperty property after FrameworkCompatibilityPreferences has been sealed - typically after Application startup, Dispatcher run, or any WPF control instantiation.

Common situations: 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.

Understand the failure class

Background: "Invalid state transition" errors: "status must be X, actually Y", "already rejected/charging/uninstalled", "cannot ... while running" — what they mean when a library rejects your call — this error's family across 31 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/f5d678c3dbba3395. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkCompatibilityPreferences.cs:155

        /// As a second example, suppose the data item normalizes an integer by
        /// capping its value to a maximum, say 100:
        ///     public int Score { set { _score = Math.Min(value, 100); } }
        /// And suppose the user types "1004".  Upon typing the 4, the binding converts
        /// string "1004" to int 1004 and sends 1004 to the data item, which stores 100.
        /// The round-trip continues, converting int 100 to string "100", which is
        /// identical to the text before typing the 4.   The TextBox reaches a state
        /// where its Text property has value "100", but it displays "1004".
        /// </notes>
        public static bool KeepTextBoxDisplaySynchronizedWithTextProperty
        {
            get { return _keepTextBoxDisplaySynchronizedWithTextProperty; }
            set
            {
                lock (_lockObject)
                {
                    if (_isSealed)
                    {
                        throw new InvalidOperationException(SR.Format(SR.CompatibilityPreferencesSealed, "AextBoxDisplaysText", "FrameworkCompatibilityPreferences"));
                    }

                    _keepTextBoxDisplaySynchronizedWithTextProperty = value;
                }
            }
        }

        internal static bool GetKeepTextBoxDisplaySynchronizedWithTextProperty()
        {
            Seal();

            return KeepTextBoxDisplaySynchronizedWithTextProperty;
        }

        #endregion KeepTextBoxDisplaySynchronizedWithTextProperty

        // There is a bug in the Windows desktop window manager which can cause
        // incorrect z-order for windows when several conditions are all met:

View on GitHub (pinned to 81131a70a4)