dotnet/wpf · error · InvalidEnumArgumentException
InvalidEnumArgumentException for value (TextTrimming)
Error message
InvalidEnumArgumentException for value (TextTrimming)
What it means
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.
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.
Example fix
// before
var trimming = (TextTrimming)intFromConfig;
formattedText.Trimming = trimming;
// after
var trimming = Enum.IsDefined(typeof(TextTrimming), intFromConfig)
? (TextTrimming)intFromConfig
: TextTrimming.CharacterEllipsis;
formattedText.Trimming = trimming; Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(TextTrimming), rawValue))
rawValue = (int)TextTrimming.CharacterEllipsis;
formattedText.Trimming = (TextTrimming)rawValue; Type guard
bool IsValidTextTrimming(int v) => v >= 0 && v <= (int)TextTrimming.WordEllipsis;
Prevention
- 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.
When it happens
Trigger: 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.
Common situations: 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.
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
- Animation_UnrecognizedHandoffBehavior
- InvalidEnumArgumentException("mode", (int)mode…
- InvalidEnumArgumentException("type", (int)type…
- InvalidEnumArgumentException("value", (int)value…
- SR.Enum_Invalid (TimeSeekOrigin)
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/8ff61a0c35161ef9.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/FormattedText.cs:1347
{
return _maxLineCount;
}
}
/// <summary>
/// Defines how omission of text is indicated.
/// CharacterEllipsis trimming allows partial words to be displayed,
/// while WordEllipsis removes whole words to fit.
/// Both guarantee to include an ellipsis ('...') at the end of the lines
/// where text has been trimmed as a result of line and column limits.
/// </summary>
public TextTrimming Trimming
{
set
{
if ((int)value < 0 || (int)value > (int)TextTrimming.WordEllipsis)
throw new InvalidEnumArgumentException("value", (int)value, typeof(TextTrimming));
_trimming = value;
if (_trimming == TextTrimming.None)
{
// if trimming is disabled, enforce emergency wrap
_defaultParaProps.SetTextWrapping(TextWrapping.Wrap);
}
else
{
_defaultParaProps.SetTextWrapping(TextWrapping.WrapWithOverflow);
}
InvalidateMetrics();
}
get
{
return _trimming;
}View on GitHub (pinned to 81131a70a4)