{"record":{"id":"e1452c2134135070","repo":"dotnet/wpf","slug":"culturesource","errorCode":null,"errorMessage":"CultureSource","messagePattern":"CultureSource","errorType":"validation","errorClass":"InvalidEnumArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/NumberSubstitution.cs","lineNumber":54,"sourceCode":"        {\n            _source = source;\n            _cultureOverride = ThrowIfInvalidCultureOverride(cultureOverride);\n            _substitution = substitution;\n        }\n\n        /// <summary>\n        /// The CultureSource property specifies how the culture for numbers\n        /// is determined. The default value is NumberCultureSource.Text,\n        /// which means the number culture is the culture of the text run.\n        /// </summary>\n        public NumberCultureSource CultureSource\n        {\n            get { return _source; }\n\n            set\n            {\n                if ((uint)value > (uint)NumberCultureSource.Override)\n                    throw new InvalidEnumArgumentException(\"CultureSource\", (int)value, typeof(NumberCultureSource));\n\n                _source = value;\n            }\n        }\n\n        /// <summary>\n        /// If the CultureSource == NumberCultureSource.Override, this \n        /// property specifies the number culture. A value of null is interpreted \n        /// as US-English. The default value is null. If CultureSource != \n        /// NumberCultureSource.Override, this property is ignored.\n        /// </summary>\n        [TypeConverter(typeof(System.Windows.CultureInfoIetfLanguageTagConverter))]\n        public CultureInfo CultureOverride\n        {\n            get { return _cultureOverride; }\n            set { _cultureOverride = ThrowIfInvalidCultureOverride(value); }\n        }\n","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/NumberSubstitution.cs#L36-L72","documentation":"NumberSubstitution.CultureSource setter validates the value against the NumberCultureSource enum range ((uint)value > NumberCultureSource.Override) and throws InvalidEnumArgumentException(\"CultureSource\", ...). Only User (0), Text (1), Override (2) are accepted.","triggerScenarios":"Assigning NumberSubstitution.CultureSource a value not defined in NumberCultureSource — an invalid cast like (NumberCultureSource)7, an uninitialized/out-of-range value read from config or serialized data.","commonSituations":"Deserializing NumberSubstitution settings from XAML/app config with an out-of-range integer; arithmetic on enum values; binding a UI control's integer value directly to the property without clamping.","solutions":["Assign only named enum members: NumberCultureSource.User, .Text, or .Override","Validate/normalize any integer before casting: if (!Enum.IsDefined(typeof(NumberCultureSource), v)) throw/fallback","Clamp values read from external config to the valid range 0..2","Fix the data source (XAML resource or settings file) that contains the out-of-range value"],"exampleFix":"// before\nnumberSubstitution.CultureSource = (NumberCultureSource)intValue; // intValue = 7\n// after\nif (Enum.IsDefined(typeof(NumberCultureSource), intValue))\n    numberSubstitution.CultureSource = (NumberCultureSource)intValue;\nelse\n    numberSubstitution.CultureSource = NumberCultureSource.AsCulture;","handlingStrategy":"validation","validationCode":"if (!Enum.IsDefined(typeof(NumberCultureSource), value)) value = NumberCultureSource.AsCulture;","typeGuard":"bool IsValidCultureSource(int v) => v >= 0 && v <= (int)NumberCultureSource.Override;","tryCatchPattern":"try { numberSubstitution.CultureSource = source; }\ncatch (InvalidEnumArgumentException ex) { log.Warn(\"Invalid CultureSource, using default\", ex); numberSubstitution.CultureSource = NumberCultureSource.AsCulture; }","preventionTips":["Only assign named enum members","Validate serialized/config integers with Enum.IsDefined before casting","Clamp UI-bound integer values to the enum range"],"tags":["wpf","textformatting","enum","numbersubstitution"],"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"}