{"record":{"id":"a947cf10e0ab3d05","repo":"lepoco/wpfui","slug":"unsupported-button-type-buttontype","errorCode":null,"errorMessage":"Unsupported button type: {buttonType}.","messagePattern":"Unsupported button type: (.+?)\\.","errorType":"exception","errorClass":"ArgumentOutOfRangeException","httpStatus":null,"severity":"error","filePath":"src/Wpf.Ui/Controls/TitleBar/TitleBarButton.cs","lineNumber":228,"sourceCode":"            return;\n        }\n\n        titleBarButton.OnButtonTypeChanged(e);\n    }\n\n    protected void OnButtonTypeChanged(DependencyPropertyChangedEventArgs e)\n    {\n        var buttonType = (TitleBarButtonType)e.NewValue;\n\n        _returnValue = buttonType switch\n        {\n            TitleBarButtonType.Unknown => PInvoke.HTNOWHERE,\n            TitleBarButtonType.Help => PInvoke.HTHELP,\n            TitleBarButtonType.Minimize => PInvoke.HTMINBUTTON,\n            TitleBarButtonType.Close => PInvoke.HTCLOSE,\n            TitleBarButtonType.Restore => PInvoke.HTMAXBUTTON,\n            TitleBarButtonType.Maximize => PInvoke.HTMAXBUTTON,\n            _ => throw new ArgumentOutOfRangeException(\n                \"e.NewValue\",\n                buttonType,\n                $\"Unsupported button type: {buttonType}.\"\n            ),\n        };\n    }\n\n    // TODO: Incorrectly calculates mouse position for high DPI displays.\n    // PresentationSource presentationSource = null;\n    // protected bool IsMouseOverElement(nint lParam)\n    // {\n    //    System.Drawing.Point winPoint;\n    //    bool gotCursorPos = User32.GetCursorPos(out winPoint);\n\n    //    if (!gotCursorPos)\n    //    {\n    //        int fallbackX = unchecked((short)((long)lParam & 0xFFFF));\n    //        int fallbackY = unchecked((short)(((long)lParam >> 16) & 0xFFFF));","sourceCodeStart":210,"sourceCodeEnd":246,"githubUrl":"https://github.com/lepoco/wpfui/blob/ffebacd61058170cf63864b7d5aa730cffff848a/src/Wpf.Ui/Controls/TitleBar/TitleBarButton.cs#L210-L246","documentation":"TitleBarButton.OnButtonTypeChanged maps each TitleBarButtonType enum value to a Windows hit-test code. The switch is exhaustive over the known enum members, so the default branch only fires when the dependency property is set to a numeric value that does not correspond to any defined TitleBarButtonType - which is possible because dependency properties accept raw values via SetValue and the code casts e.NewValue directly. This is essentially an unreachable-in-normal-use defensive guard against an invalid/corrupted enum value.","triggerScenarios":"Calling button.SetValue(TitleBarButton.ButtonTypeProperty, (TitleBarButtonType)999) or otherwise assigning a numeric value outside the enum's defined range; future enum members added without updating the switch; XAML binding that produces an out-of-range value.","commonSituations":"Programmatic misuse via SetValue with a cast; deserialised/bound values that fall outside the enum; version skew where a newer enum value is bound against an older library build.","solutions":["Only assign defined TitleBarButtonType values (Unknown, Help, Minimize, Close, Restore, Maximize).","Validate bound values before assignment; clamp unknown values to Unknown.","Update WPF UI to a version whose switch covers the enum value you are binding."],"exampleFix":"// before\nbutton.SetValue(TitleBarButton.ButtonTypeProperty, (TitleBarButtonType)42);\n\n// after\nbutton.ButtonType = TitleBarButtonType.Unknown;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(TitleBarButtonType), value))\n{\n    throw new ArgumentOutOfRangeException(nameof(value), \"Invalid TitleBarButtonType.\");\n}","typeGuard":"static bool IsDefinedButtonType(TitleBarButtonType v) => Enum.IsDefined(v);","tryCatchPattern":"try { button.ButtonType = (TitleBarButtonType)raw; }\ncatch (ArgumentOutOfRangeException ex) when (ex.Message.Contains(\"Unsupported button type\"))\n{\n    _logger.LogError(ex, \"Invalid TitleBarButtonType {Raw}\", raw);\n    button.ButtonType = TitleBarButtonType.Unknown;\n}","preventionTips":["Only assign defined TitleBarButtonType enum members.","Validate external/deserialised values with Enum.IsDefined before assignment.","Keep the library version current so new enum members are covered."],"tags":["titlebar","enum","argument-out-of-range","wpf"],"backgroundTag":null,"analyzedSha":"ffebacd61058170cf63864b7d5aa730cffff848a","analyzedAt":"2026-08-13T21:36:01.370Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}