{"record":{"id":"8ff61a0c35161ef9","repo":"dotnet/wpf","slug":"invalidenumargumentexception-for-value-texttrimming","errorCode":null,"errorMessage":"InvalidEnumArgumentException for value (TextTrimming)","messagePattern":"InvalidEnumArgumentException for value \\(TextTrimming\\)","errorType":"exception","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FormattedText.cs","lineNumber":1347,"sourceCode":"            {\n                return _maxLineCount;\n            }\n        }\n\n\n        /// <summary>\n        /// Defines how omission of text is indicated.\n        /// CharacterEllipsis trimming allows partial words to be displayed,\n        /// while WordEllipsis removes whole words to fit.\n        /// Both guarantee to include an ellipsis ('...') at the end of the lines\n        /// where text has been trimmed as a result of line and column limits.\n        /// </summary>\n        public TextTrimming Trimming\n        {\n            set\n            {\n                if ((int)value < 0 || (int)value > (int)TextTrimming.WordEllipsis)\n                    throw new InvalidEnumArgumentException(\"value\", (int)value, typeof(TextTrimming));\n\n                _trimming = value;\n                if (_trimming == TextTrimming.None)\n                {\n                    // if trimming is disabled, enforce emergency wrap\n                    _defaultParaProps.SetTextWrapping(TextWrapping.Wrap);\n                }\n                else \n                {\n                    _defaultParaProps.SetTextWrapping(TextWrapping.WrapWithOverflow);\n                }\n\n                InvalidateMetrics();\n            }\n            get\n            {\n                return _trimming;\n            }","sourceCodeStart":1329,"sourceCodeEnd":1365,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FormattedText.cs#L1329-L1365","documentation":"FormattedText.Trimming setter throws InvalidEnumArgumentException when the assigned TextTrimming value is outside the defined enum range (less than 0 or greater than TextTrimming.WordEllipsis). The library validates the raw int to catch invalid casts of arbitrary integers into the enum.","triggerScenarios":"Assigning ((TextTrimming)someInt) where someInt is negative or larger than (int)TextTrimming.WordEllipsis, e.g., casting a raw config value, deserialized number, or another enum directly to TextTrimming.","commonSituations":"Mapping a legacy or external setting (int from an INI/registry/database) to TextTrimming; casting TextWrapping or another unrelated enum to TextTrimming; upgraded enum values from a different WPF version not recognized by the current range check.","solutions":["Validate the integer with Enum.IsDefined(typeof(TextTrimming), value) before casting and assigning.","Fall back to a default such as TextTrimming.CharacterEllipsis when the source value is out of range.","Fix the source of the bad integer so only valid TextTrimming members are produced."],"exampleFix":"// before\nvar trimming = (TextTrimming)intFromConfig;\nformattedText.Trimming = trimming;\n\n// after\nvar trimming = Enum.IsDefined(typeof(TextTrimming), intFromConfig)\n    ? (TextTrimming)intFromConfig\n    : TextTrimming.CharacterEllipsis;\nformattedText.Trimming = trimming;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(TextTrimming), rawValue))\n    rawValue = (int)TextTrimming.CharacterEllipsis;\nformattedText.Trimming = (TextTrimming)rawValue;","typeGuard":"bool IsValidTextTrimming(int v) => v >= 0 && v <= (int)TextTrimming.WordEllipsis;","tryCatchPattern":null,"preventionTips":["Always pass through Enum.IsDefined when converting raw integers to TextTrimming.","Never cast unrelated enums (e.g., TextWrapping) into TextTrimming.","Persist enum names, not numeric values, in configuration to survive enum changes."],"tags":["wpf","enum","invalid-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"}