dotnet/wpf · error · InvalidEnumArgumentException

value

Error message

value

What it means

The RenderMode property setter only accepts RenderMode.Default or RenderMode.SoftwareOnly; anything else is not accessible (partly-trust hardening aside, hardware values are set through other paths). An out-of-range value throws InvalidEnumArgumentException with the parameter name 'value'.

Solutions

  1. Assign only RenderMode.Default or RenderMode.SoftwareOnly to HwndTarget.RenderMode
  2. Validate/sanitize any deserialized int against known RenderMode values before casting
  3. Use RenderMode.Default to let WPF pick hardware when supported

Example fix

// before
HwndTarget.RenderMode = (RenderMode)readFromConfig; // may throw
// after
var mode = (RenderMode)readFromConfig;
HwndTarget.RenderMode = (mode == RenderMode.Default || mode == RenderMode.SoftwareOnly) ? mode : RenderMode.Default;
Defensive patterns

Strategy: validation

Validate before calling

if (value is not (RenderMode.Default or RenderMode.SoftwareOnly)) value = RenderMode.Default;

Type guard

bool IsValidRenderMode(RenderMode m) => m is RenderMode.Default or RenderMode.SoftwareOnly;

Try / catch

try { HwndTarget.RenderMode = mode; } catch (InvalidEnumArgumentException) { HwndTarget.RenderMode = RenderMode.Default; }

Prevention

When it happens

Trigger: Assigning RenderMode.Hardware or RenderMode.HardwareReference directly to HwndTarget.RenderMode, or casting an arbitrary int to RenderMode and assigning it.

Common situations: Persisting an enum as int in config and round-tripping it back; assuming Hardware is assignable like Default/SoftwareOnly.

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


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ece19cd88d7880b5. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/InterOp/HwndTarget.cs:658

        ///     <para/>
        ///     Callers must have UIPermission(UIPermissionWindow.AllWindows) to set this property.
        /// </remarks>
        public RenderMode RenderMode
        {
            get
            {
                return _renderModePreference;
            }

            // Note: We think it is safe to expose this in partial trust, but doing so would suggest
            // we should also expose HwndSource (the only way to get to the HwndTarget instance).
            // We don't want to bite off that much exposure at this point in the product, so we enforce
            // that this is not accessible from partial trust for now.
            set
            {
                if (value != RenderMode.Default && value != RenderMode.SoftwareOnly)
                {
                    throw new System.ComponentModel.InvalidEnumArgumentException("value", (int)value, typeof(RenderMode));
                }

                _renderModePreference = value;

                InvalidateRenderMode();
            }
        }

        /// <summary>
        /// Dispose cleans up the state associated with HwndTarget.
        /// </summary>
        public override void Dispose()
        {
           // Its outside the try finally block because we want the exception to be
           // thrown if we are on a different thread and we don't want to call Dispose
           // on base class in that case.
           VerifyAccess();

View on GitHub (pinned to 81131a70a4)