{"record":{"id":"660ccdf6a83181db","repo":"dotnet/wpf","slug":"sr-unexpectedwindowstate-state","errorCode":null,"errorMessage":"SR.UnexpectedWindowState (state)","messagePattern":"SR\\.UnexpectedWindowState \\(state\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"warning","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs","lineNumber":376,"sourceCode":"                    if (((IWindowProvider)this).VisualState == WindowVisualState.Maximized)\n                    {\n                        return;\n                    }\n\n                    ClearMenuMode();\n\n                    if (!Misc.PostMessage(_hwnd, UnsafeNativeMethods.WM_SYSCOMMAND, (IntPtr)UnsafeNativeMethods.SC_MAXIMIZE, IntPtr.Zero))\n                    {\n                        throw new InvalidOperationException(SR.OperationCannotBePerformed);\n                    }\n\n                    return;\n                }\n\n                default:\n                {\n                    Debug.Fail(\"unexpected switch() case:\");\n                    throw new ArgumentException(SR.UnexpectedWindowState,nameof(state));\n                }\n\n            }\n\n        }\n\n        void IWindowProvider.Close()\n        {\n            ClearMenuMode();\n\n            if (!Misc.PostMessage(_hwnd, UnsafeNativeMethods.WM_SYSCOMMAND, (IntPtr)UnsafeNativeMethods.SC_CLOSE, IntPtr.Zero))\n            {\n                throw new InvalidOperationException(SR.OperationCannotBePerformed);\n            }\n        }\n\n        bool IWindowProvider.WaitForInputIdle( int milliseconds )\n        {","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/MS/Internal/Automation/HwndProxyElementProvider.cs#L358-L394","documentation":"SetVisualState only handles Normal, Minimized, and Maximized; any other WindowVisualState value (there are no other public values, so this is a defensive default) reaches the default branch, which fires Debug.Fail and throws ArgumentException(SR.UnexpectedWindowState, nameof(state)) at HwndProxyElementProvider.cs:376.","triggerScenarios":"Passing an undefined/out-of-range WindowVisualState cast or an invalid value obtained from interop/reflection into IWindowProvider.SetVisualState — the enum value is not one of Normal(0)/Minimized(1)/Maximized(2).","commonSituations":"Code that stores window state as int and casts without validation; binding layers or COM callers passing raw integers; passing WindowBroken/WindowMinimizedTray-like values from other Windows APIs into the UIA enum by mistake.","solutions":["Validate the value is a defined WindowVisualState member with Enum.IsDefined before calling SetVisualState.","Fix the casting code to use the enum type end-to-end instead of int with unchecked casts.","Catch ArgumentException and log the offending value to identify the source of the bad enum.","If interoperating, map the foreign state value to the closest supported WindowVisualState before the call."],"exampleFix":"// before\nvar state = (WindowVisualState)rawValue; // rawValue may be out of range\nwindowPattern.SetVisualState(state);\n\n// after\nif (Enum.IsDefined(typeof(WindowVisualState), rawValue))\n{\n    windowPattern.SetVisualState((WindowVisualState)rawValue);\n}","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(WindowVisualState), state)) throw new ArgumentOutOfRangeException(nameof(state), state, \"Not a valid WindowVisualState\");","typeGuard":"static bool IsValidWindowVisualState(object v) => v is int i && Enum.IsDefined(typeof(WindowVisualState), i);","tryCatchPattern":"try { windowPattern.SetVisualState(state); } catch (ArgumentException ex) { log($\"Bad WindowVisualState value: {state}\"); }","preventionTips":["Use the WindowVisualState enum type end-to-end; avoid int casts without Enum.IsDefined","Map foreign API state values to WindowVisualState explicitly before interop calls","Enable checked enum parsing at boundaries (parsing, COM, reflection)","Never pass raw integers from Win32 APIs directly as WindowVisualState"],"tags":["uiautomation","window-pattern","invalid-enum","argument"],"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"}