dotnet/wpf · error · ArgumentException

SR.UnknownExpressionMode

Error message

SR.UnknownExpressionMode

What it means

Expression's constructor takes a mode controlling whether the expression is shareable, forwards invalidations, or supports unbound sources. The mode argument is validated against a known set of ExpressionMode values; any unknown/undefined mode reaches the default branch and throws ArgumentException(SR.UnknownExpressionMode). This is an internal-facing guard so bindings built with corrupted or undefined modes fail immediately.

Solutions

  1. Pass a defined ExpressionMode constant (e.g. ExpressionMode.Shareable, ExpressionMode.ForwardsInvalidations, ExpressionMode.SupportsUnboundSources)
  2. Validate with Enum.IsDefined before constructing
  3. Check that the flag combination used is one the Expression constructor supports
  4. Pin/align WindowsBase versions across the app to avoid changed enum values

Example fix

// before
var expr = new MyExpression((ExpressionMode)0x4000);
// after
var expr = new MyExpression(ExpressionMode.ForwardsInvalidations);
Defensive patterns

Strategy: validation

Validate before calling

static bool IsValidExpressionMode(ExpressionMode m) => m is ExpressionMode.None or ExpressionMode.Shareable or ExpressionMode.ForwardsInvalidations or ExpressionMode.SupportsUnboundSources;

Type guard

static bool IsDefinedEnum<TEnum>(object v) where TEnum : struct, Enum => Enum.IsDefined(typeof(TEnum), v);

Try / catch

try { var expr = new MyExpression(mode); }
catch (ArgumentException ex) { log.Error($"Unknown expression mode: {ex.Message}"); }

Prevention

When it happens

Trigger: Constructing an Expression (e.g. via Binding internals or a subclass like ObjectRef/BindingExpression plumbing) passing an ExpressionMode value that is not one of the defined flag combinations, typically via an invalid cast of an int.

Common situations: Low-level WPF binding code or custom expression subclasses passing wrong enum constants; version mismatches where a mode constant changed between WindowsBase builds; tests exercising internal Expression constructors with bad flags.

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/7cf9acd7c3340edd. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/Expression.cs:110

                    break;

                case ExpressionMode.NonSharable:
                    _flags |= InternalFlags.NonShareable;
                    break;

                case ExpressionMode.ForwardsInvalidations:
                    _flags |= InternalFlags.ForwardsInvalidations;
                    _flags |= InternalFlags.NonShareable;
                    break;

                case ExpressionMode.SupportsUnboundSources:
                    _flags |= InternalFlags.ForwardsInvalidations;
                    _flags |= InternalFlags.NonShareable;
                    _flags |= InternalFlags.SupportsUnboundSources;
                    break;

                default:
                    throw new ArgumentException(SR.UnknownExpressionMode);
            }
        }


        // We need this Clone method to copy a binding during Freezable.Copy.  We shouldn't be taking
        // the target object/dp parameters here, but Binding.ProvideValue requires it.  (Binding
        // could probably be re-factored so that we don't need this).
        internal virtual Expression Copy( DependencyObject targetObject, DependencyProperty targetDP )
        {
            // By default, just use the same copy.
            return this;
        }


        /// <summary>
        ///     List of sources of the Expression
        /// </summary>
        /// <returns>Sources list</returns>

View on GitHub (pinned to 81131a70a4)