{"record":{"id":"a8d953d025a799c8","repo":"dotnet/wpf","slug":"sr-incorrectflowdirection","errorCode":null,"errorMessage":"SR.IncorrectFlowDirection","messagePattern":"SR\\.IncorrectFlowDirection","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs","lineNumber":6822,"sourceCode":"                {\n                    icon.Dispose();\n                }\n            }\n        }\n\n        private void CreateRtl()\n        {\n            if ( this.FlowDirection == FlowDirection.LeftToRight )\n            {\n                _StyleEx &= ~NativeMethods.WS_EX_LAYOUTRTL;\n            }\n            else if ( this.FlowDirection == FlowDirection.RightToLeft )\n            {\n                _StyleEx |= NativeMethods.WS_EX_LAYOUTRTL;\n            }\n            else\n            {\n                throw new InvalidOperationException(SR.IncorrectFlowDirection);\n            }\n}\n\n        /// <summary>\n        ///     Updates both style and styleEx for the window\n        ///\n        ///     WCP: Figure out how to update window styles after calling SetWindowLong\n        ///     Currently hides the window and then shows it to make it update. Have to\n        ///     find a better way of doing this.\n        /// </summary>\n        internal void Flush()\n        {\n            // (A NullReferenceException occurs when animating\n            // the WindowStyle enum via a custom animation).  This bug contains details of\n            // why we were seeing the null ref.\n            //\n            // Sometimes, the SetWindowPos call below results in sending certain window messages\n            // like (WM_SIZE) and their handling leads to setting some property on the Window leading","sourceCodeStart":6804,"sourceCodeEnd":6840,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Window.cs#L6804-L6840","documentation":"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).","triggerScenarios":"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.","commonSituations":"Persisting/restoring settings that store FlowDirection as an int and casting without validation, reflection-based code setting invalid enum values, corrupted XAML/binding sources.","solutions":["Validate any persisted/cast value against Enum.IsDefined(typeof(FlowDirection), value) before assigning","Ensure FlowDirection is only set to FlowDirection.LeftToRight or FlowDirection.RightToLeft","Fix the data source (settings file/binding) that supplies the invalid enum value"],"exampleFix":"// before\nwindow.FlowDirection = (FlowDirection)savedInt;\n// after\nwindow.FlowDirection = Enum.IsDefined(typeof(FlowDirection), savedInt)\n    ? (FlowDirection)savedInt : FlowDirection.LeftToRight;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(FlowDirection), rawValue)) rawValue = FlowDirection.LeftToRight;","typeGuard":"static bool IsValidFlowDirection(object v) => v is FlowDirection fd && (fd == FlowDirection.LeftToRight || fd == FlowDirection.RightToLeft);","tryCatchPattern":"try { ApplyWindowStyle(); } catch (InvalidOperationException ex) when (ex.Message.Contains(\"FlowDirection\")) { window.FlowDirection = FlowDirection.LeftToRight; }","preventionTips":["Never cast raw ints to FlowDirection without Enum.IsDefined","Only assign the two defined FlowDirection constants","Validate settings files that persist enum values"],"tags":["wpf","window","flowdirection","invalid-enum"],"backgroundTag":"invalid-enum-value","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"}