{"record":{"id":"cef72afa868635a2","repo":"dotnet/wpf","slug":"direction","errorCode":null,"errorMessage":"direction","messagePattern":"direction","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Input/KeyboardNavigation.cs","lineNumber":2546,"sourceCode":"\n        private double GetPerpDistance(Rect sourceRect, Rect targetRect, FocusNavigationDirection direction)\n        {\n            switch (direction)\n            {\n                case FocusNavigationDirection.Right :\n                    return targetRect.Left - sourceRect.Left;\n\n                case FocusNavigationDirection.Left :\n                    return sourceRect.Right - targetRect.Right;\n\n                case FocusNavigationDirection.Up :\n                    return sourceRect.Bottom - targetRect.Bottom;\n\n                case FocusNavigationDirection.Down :\n                    return targetRect.Top - sourceRect.Top;\n\n                default :\n                    throw new System.ComponentModel.InvalidEnumArgumentException(\"direction\", (int)direction, typeof(FocusNavigationDirection));\n            }\n        }\n\n        // Example when moving down:\n        // distance between sourceRect.TopLeft (or Y=vertical baseline)\n        // and targetRect.TopLeft\n        private double GetDistance(Rect sourceRect, Rect targetRect, FocusNavigationDirection direction)\n        {\n            Point startPoint;\n            Point endPoint;\n            switch (direction)\n            {\n                case FocusNavigationDirection.Right :\n                    startPoint = sourceRect.TopLeft;\n                    if (_horizontalBaseline != BASELINE_DEFAULT)\n                        startPoint.Y = _horizontalBaseline;\n                    endPoint = targetRect.TopLeft;\n                    break;","sourceCodeStart":2528,"sourceCodeEnd":2564,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Input/KeyboardNavigation.cs#L2528-L2564","documentation":"A KeyboardNavigation helper computes a signed distance metric between rects keyed on FocusNavigationDirection; the switch covers Left/Right/Up/Down and the default arm throws InvalidEnumArgumentException for \"direction\" because the message is the parameter name. It means an undefined/unsupported FocusNavigationDirection value reached keyboard navigation.","triggerScenarios":"Calling KeyboardNavigation APIs (or internal distance helpers via PredictFocus-like paths) with (FocusNavigationDirection)someInt where the int is not a defined member, or a direction value from another schema/older enum that maps outside the four handled cases.","commonSituations":"Raw integer casts from persisted settings, interop, or key-mapping tables; deserializing direction from config; binding UI controls to enum values stored as ints that no longer match the enum layout.","solutions":["Validate before use: if (!Enum.IsDefined(typeof(FocusNavigationDirection), direction)) reject/default it.","Replace raw casts with an explicit, defaulted switch mapping ints to valid directions.","Persist the direction as its name string instead of a numeric value.","If the direction is genuinely unsupported (e.g., a diagonal), choose the nearest supported axis direction or skip navigation."],"exampleFix":"// before\nMoveFocus((FocusNavigationDirection)storedInt);\n\n// after\nif (Enum.IsDefined(typeof(FocusNavigationDirection), storedInt))\n    MoveFocus((FocusNavigationDirection)storedInt);\nelse\n    MoveFocus(FocusNavigationDirection.Down); // safe default","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(FocusNavigationDirection), direction))\n    direction = FocusNavigationDirection.Down; // or reject the input\nnavigation.MoveFocus(direction);","typeGuard":"bool IsDefinedFocusDirection(FocusNavigationDirection d) => Enum.IsDefined(typeof(FocusNavigationDirection), d);","tryCatchPattern":"try { MoveFocus(direction); }\ncatch (System.ComponentModel.InvalidEnumArgumentException ex) when (ex.ParamName == \"direction\")\n{ MoveFocus(FocusNavigationDirection.Down); }","preventionTips":["Never cast raw ints to FocusNavigationDirection without Enum.IsDefined.","Persist direction values as names, not numbers.","Constrain UI/config inputs to the defined direction set with a whitelist.","Handle diagonals/unsupported directions explicitly before calling navigation APIs."],"tags":["wpf","keyboardnavigation","focusnavigationdirection","invalidenumargumentexception","enum"],"backgroundTag":"invalid-enum-argument","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"}