{"record":{"id":"c1c8289710585996","repo":"dotnet/wpf","slug":"invalidenumargumentexception-textformattingmode-int","errorCode":null,"errorMessage":"InvalidEnumArgumentException(\"textFormattingMode\", (int)textFormattingMode, typeof(TextFormattingMode))","messagePattern":"InvalidEnumArgumentException\\(\"textFormattingMode\", \\(int\\)textFormattingMode, typeof\\(TextFormattingMode\\)\\)","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextFormatter.cs","lineNumber":45,"sourceCode":"    /// for international text layout.\n    /// \n    /// Unlike traditional text APIs, the TextFormatter interacts with a text layout client \n    /// through a set of callback methods. It requires the client to provide these methods \n    /// in an implementation of the TextSource class.\n    /// </summary>\n    public abstract class TextFormatter : IDisposable\n    {\n        private static readonly object _staticLock = new object();\n\n        /// <summary>\n        /// Client to create a new instance of TextFormatter\n        /// </summary>\n        /// <returns>New instance of TextFormatter</returns>\n        public static TextFormatter Create(TextFormattingMode textFormattingMode)\n        {\n            if ((int)textFormattingMode < 0 || (int)textFormattingMode > 1)\n            {\n                throw new System.ComponentModel.InvalidEnumArgumentException(\"textFormattingMode\", (int)textFormattingMode, typeof(TextFormattingMode));\n            }\n            \n            // create a new instance of TextFormatter which allows the use of multiple contexts.\n            return new TextFormatterImp(textFormattingMode);\n        }\n\n        /// <summary>\n        /// Client to create a new instance of TextFormatter\n        /// </summary>\n        /// <returns>New instance of TextFormatter</returns>\n        public static TextFormatter Create()\n        {\n            // create a new instance of TextFormatter which allows the use of multiple contexts.\n            return new TextFormatterImp();\n        }\n\n\n        /// <summary>","sourceCodeStart":27,"sourceCodeEnd":63,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/TextFormatter.cs#L27-L63","documentation":"TextFormatter.Create validates textFormattingMode against the defined TextFormattingMode values (Ideal=0, Display=1); anything outside 0..1 throws InvalidEnumArgumentException(\"textFormattingMode\", ...). The mode chooses GDI-compatible or ideal metrics for text layout.","triggerScenarios":"Calling TextFormatter.Create with an invalid cast value, e.g. TextFormatter.Create((TextFormattingMode)5), or a mode read from config/serialization that is not 0 or 1.","commonSituations":"Persisting the formatting mode as an int in settings and restoring an out-of-range value; arithmetic or flags misuse on the enum; older code paths using removed enum members.","solutions":["Call Create only with TextFormattingMode.Ideal or TextFormattingMode.Display","Validate ints before casting: Enum.IsDefined(typeof(TextFormattingMode), v), else default to Ideal","Fix or clamp the config value feeding the mode; default to Display for GDI-compatible metrics in DPI-heavy apps","Catch InvalidEnumArgumentException around Create and retry with TextFormatter.Create(TextFormattingMode.Ideal)"],"exampleFix":"// before\nvar formatter = TextFormatter.Create((TextFormattingMode)modeInt); // modeInt = 5\n// after\nif (!Enum.IsDefined(typeof(TextFormattingMode), modeInt)) modeInt = (int)TextFormattingMode.Display;\nvar formatter = TextFormatter.Create((TextFormattingMode)modeInt);","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(TextFormattingMode), modeInt)) modeInt = (int)TextFormattingMode.Ideal;","typeGuard":"bool IsValidTextFormattingMode(int v) => v == (int)TextFormattingMode.Ideal || v == (int)TextFormattingMode.Display;","tryCatchPattern":"try { formatter = TextFormatter.Create(mode); }\ncatch (InvalidEnumArgumentException ex) { log.Warn(\"Invalid TextFormattingMode, using Display\", ex); formatter = TextFormatter.Create(TextFormattingMode.Display); }","preventionTips":["Only pass TextFormattingMode.Ideal or .Display to Create","Validate settings/config integers with Enum.IsDefined before casting","Default to Display for pixel-aligned UI text; Ideal for measurement-heavy scenarios"],"tags":["wpf","textformatting","enum","textformatter"],"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"}