dotnet/wpf · error · InvalidOperationException

SR.IncorrectFlowDirection

Error message

SR.IncorrectFlowDirection

What it means

During style creation Window maps its FlowDirection to Win32 extended styles: LTR maps to no flag, RightToLeft maps to WS_EX_LAYOUTRTL. Any other value cannot occur with a valid enum, so the code treats it as an impossible state and throws InvalidOperationException. Hitting it means the FlowDirection value is invalid or corrupted (e.g. an out-of-range cast).

Solutions

  1. Validate any persisted/cast value against Enum.IsDefined(typeof(FlowDirection), value) before assigning
  2. Ensure FlowDirection is only set to FlowDirection.LeftToRight or FlowDirection.RightToLeft
  3. Fix the data source (settings file/binding) that supplies the invalid enum value

Example fix

// before
window.FlowDirection = (FlowDirection)savedInt;
// after
window.FlowDirection = Enum.IsDefined(typeof(FlowDirection), savedInt)
    ? (FlowDirection)savedInt : FlowDirection.LeftToRight;
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(FlowDirection), rawValue)) rawValue = FlowDirection.LeftToRight;

Type guard

static bool IsValidFlowDirection(object v) => v is FlowDirection fd && (fd == FlowDirection.LeftToRight || fd == FlowDirection.RightToLeft);

Try / catch

try { ApplyWindowStyle(); } catch (InvalidOperationException ex) when (ex.Message.Contains("FlowDirection")) { window.FlowDirection = FlowDirection.LeftToRight; }

Prevention

When it happens

Trigger: Window._StyleEx computation when this.FlowDirection is neither LeftToRight nor RightToLeft — typically only via an invalid enum cast like (FlowDirection)42, corrupted databinding, or deserialized invalid enum data.

Common situations: Persisting/restoring settings that store FlowDirection as an int and casting without validation, reflection-based code setting invalid enum values, corrupted XAML/binding sources.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

                {
                    icon.Dispose();
                }
            }
        }

        private void CreateRtl()
        {
            if ( this.FlowDirection == FlowDirection.LeftToRight )
            {
                _StyleEx &= ~NativeMethods.WS_EX_LAYOUTRTL;
            }
            else if ( this.FlowDirection == FlowDirection.RightToLeft )
            {
                _StyleEx |= NativeMethods.WS_EX_LAYOUTRTL;
            }
            else
            {
                throw new InvalidOperationException(SR.IncorrectFlowDirection);
            }
}

        /// <summary>
        ///     Updates both style and styleEx for the window
        ///
        ///     WCP: Figure out how to update window styles after calling SetWindowLong
        ///     Currently hides the window and then shows it to make it update. Have to
        ///     find a better way of doing this.
        /// </summary>
        internal void Flush()
        {
            // (A NullReferenceException occurs when animating
            // the WindowStyle enum via a custom animation).  This bug contains details of
            // why we were seeing the null ref.
            //
            // Sometimes, the SetWindowPos call below results in sending certain window messages
            // like (WM_SIZE) and their handling leads to setting some property on the Window leading

View on GitHub (pinned to 81131a70a4)