dotnet/wpf · error · InvalidOperationException

SR.ChangeNotAllowedAfterShow

Error message

SR.ChangeNotAllowedAfterShow

What it means

Window.CoerceAllowsTransparency is the dependency-property coercion callback for AllowsTransparency. Once the window's HWND (source window) has been created - i.e. the window has been shown - WPF forbids changing AllowsTransparency and throws InvalidOperationException(SR.ChangeNotAllowedAfterShow). The value affects the native window style at creation time only.

Solutions

  1. Set AllowsTransparency in the Window constructor before Show/ShowDialog
  2. If runtime change is needed, close the window, change the value, and create/show a new window instance
  3. Remove bindings/styles that mutate AllowsTransparency after window creation
  4. Check WindowInteropHelper(window).Handle availability / IsSourceWindowNull before assigning

Example fix

// before
public MainWindow() { InitializeComponent(); }
private void ToggleTransparency() {
    AllowsTransparency = true; // throws after Show()
}
// after
public MainWindow() {
    InitializeComponent();
    AllowsTransparency = WindowStyle == WindowStyle.None;
    Background = Brushes.Transparent;
}
Defensive patterns

Strategy: validation

Validate before calling

if (window.IsLoaded) { throw new InvalidOperationException("Set AllowsTransparency before Show()"); }
window.AllowsTransparency = desired;

Type guard

bool CanSetAllowsTransparency(Window w) => !w.IsLoaded;

Try / catch

try { window.AllowsTransparency = value; } catch (InvalidOperationException ex) when (ex.Message == SR.ChangeNotAllowedAfterShow) { /* recreate window with new value */ }

Prevention

When it happens

Trigger: Setting AllowsTransparency (directly or via a style/binding/coercion) after Show(), ShowDialog(), or the window handle has been created; binding AllowsTransparency to a value that changes at runtime.

Common situations: Toggling transparency at runtime to switch chrome modes; a data-bound AllowsTransparency that updates after the window loads; setting the property in a Loaded handler instead of the constructor.

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/d8101b908f205ace. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs:794

        /// Whether or not the Window uses per-pixel opacity
        /// </summary>
        public bool AllowsTransparency
        {
            get { return (bool)GetValue(AllowsTransparencyProperty); }
            set { SetValue(AllowsTransparencyProperty, BooleanBoxes.Box(value)); }
        }

        private static void OnAllowsTransparencyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
        }

        private static object CoerceAllowsTransparency(DependencyObject d, object value)
        {
            value = VerifyAccessCoercion(d, value);

            if (!((Window) d).IsSourceWindowNull)
            {
                throw new InvalidOperationException(SR.ChangeNotAllowedAfterShow);
            }

            return value;
        }

        /// <summary>
        ///     The DependencyProperty for TitleProperty.
        ///     Flags:              None
        ///     Default Value:      String.Empty
        /// </summary>
        public static readonly DependencyProperty TitleProperty =
                DependencyProperty.Register("Title", typeof(String), typeof(Window),
                        new FrameworkPropertyMetadata(String.Empty,
                                new PropertyChangedCallback(_OnTitleChanged)),
                        new ValidateValueCallback(_ValidateText));
        /// <summary>
        ///     The data that will be displayed as the title of the window.
        ///     Hosts are free to display the title in any manner that they

View on GitHub (pinned to 81131a70a4)