dotnet/wpf · error
InvalidEnumArgumentException("value", (int)value…
Error message
InvalidEnumArgumentException("value", (int)value, typeof(XamlWriterMode)) What it means
XamlDesignerSerializationManager.XamlWriterMode validates the assigned value with IsValidXamlWriterMode and throws InvalidEnumArgumentException when the value is not a defined XamlWriterMode member, preventing designers from entering an unknown serialization mode.
Solutions
- Only assign values from the XamlWriterMode enum (e.g. XamlWriterMode.Expression)
- Validate raw int values against Enum.IsDefined(typeof(XamlWriterMode), v) before casting
- Fix config parsing so the stored mode maps to a defined enum member
Example fix
// before
manager.XamlWriterMode = (XamlWriterMode)42;
// after
XamlWriterMode mode;
if (Enum.TryParse("Expression", out mode))
manager.XamlWriterMode = mode; Defensive patterns
Strategy: validation
Validate before calling
if (!Enum.IsDefined(typeof(XamlWriterMode), value))
throw new InvalidEnumArgumentException(nameof(value), (int)value, typeof(XamlWriterMode)); Type guard
bool IsValidXamlWriterMode(XamlWriterMode m) => Enum.IsDefined(typeof(XamlWriterMode), m);
Try / catch
try { manager.XamlWriterMode = mode; }
catch (InvalidEnumArgumentException ex) { /* use a defined XamlWriterMode member */ } Prevention
- Assign only named XamlWriterMode members, never raw ints
- Use Enum.IsDefined/Enum.TryParse before casting persisted values
- Keep enum types consistent across designer tooling
When it happens
Trigger: Setting XamlDesignerSerializationManager.XamlWriterMode to an undefined XamlWriterMode value, e.g. an invalid cast from int or a value from another enum.
Common situations: Custom designer tooling casting raw ints to XamlWriterMode; storing the mode in config and converting without validation; copy-paste from code using a different enum.
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_ChildMustBeKeyFrame
- Animation_UnrecognizedHandoffBehavior
- InvalidEnumArgumentException for value (TextTrimming)
- InvalidEnumArgumentException("mode", (int)mode…
- InvalidEnumArgumentException("type", (int)type…
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/5a542e517e774c4e.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/XamlDesignerSerializationManager.cs:73
#region Properties
/// <summary>
/// The mode of serialization for
/// all Expressions
/// </summary>
public XamlWriterMode XamlWriterMode
{
get
{
return _xamlWriterMode;
}
set
{
// Validate Input Arguments
if (!IsValidXamlWriterMode(value))
{
throw new InvalidEnumArgumentException("value", (int)value, typeof(XamlWriterMode));
}
_xamlWriterMode = value;
}
}
/// <summary>
/// XmlWriter
/// </summary>
internal XmlWriter XmlWriter
{
get { return _xmlWriter; }
}
#endregion Properties
#region Internal Methods
View on GitHub (pinned to 81131a70a4)