dotnet/wpf · error · InvalidEnumArgumentException
CultureSource
Error message
CultureSource
What it means
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.
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
Example fix
// before
numberSubstitution.CultureSource = (NumberCultureSource)intValue; // intValue = 7
// after
if (Enum.IsDefined(typeof(NumberCultureSource), intValue))
numberSubstitution.CultureSource = (NumberCultureSource)intValue;
else
numberSubstitution.CultureSource = NumberCultureSource.AsCulture; Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(NumberCultureSource), value)) value = NumberCultureSource.AsCulture;
Type guard
bool IsValidCultureSource(int v) => v >= 0 && v <= (int)NumberCultureSource.Override;
Try / catch
try { numberSubstitution.CultureSource = source; }
catch (InvalidEnumArgumentException ex) { log.Warn("Invalid CultureSource, using default", ex); numberSubstitution.CultureSource = NumberCultureSource.AsCulture; } Prevention
- Only assign named enum members
- Validate serialized/config integers with Enum.IsDefined before casting
- Clamp UI-bound integer values to the enum range
When it happens
Trigger: 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.
Common situations: 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.
Understand the failure class
Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.
Related errors
- InvalidEnumArgumentException("Substitution", (int)value…
- InvalidEnumArgumentException("textFormattingMode"…
- SR.Format(SR.Enum_Invalid, typeof(TextMarkerStyle))
- Animation_UnrecognizedHandoffBehavior
- args
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/e1452c2134135070.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/textformatting/NumberSubstitution.cs:54
{
_source = source;
_cultureOverride = ThrowIfInvalidCultureOverride(cultureOverride);
_substitution = substitution;
}
/// <summary>
/// The CultureSource property specifies how the culture for numbers
/// is determined. The default value is NumberCultureSource.Text,
/// which means the number culture is the culture of the text run.
/// </summary>
public NumberCultureSource CultureSource
{
get { return _source; }
set
{
if ((uint)value > (uint)NumberCultureSource.Override)
throw new InvalidEnumArgumentException("CultureSource", (int)value, typeof(NumberCultureSource));
_source = value;
}
}
/// <summary>
/// If the CultureSource == NumberCultureSource.Override, this
/// property specifies the number culture. A value of null is interpreted
/// as US-English. The default value is null. If CultureSource !=
/// NumberCultureSource.Override, this property is ignored.
/// </summary>
[TypeConverter(typeof(System.Windows.CultureInfoIetfLanguageTagConverter))]
public CultureInfo CultureOverride
{
get { return _cultureOverride; }
set { _cultureOverride = ThrowIfInvalidCultureOverride(value); }
}
View on GitHub (pinned to 81131a70a4)