dotnet/wpf · error · ArgumentException

SR.Format(SR.DefaultValueInvalid, propertyName)

Error message

SR.Format(SR.DefaultValueInvalid, propertyName)

What it means

When PropertyMetadata supplies a default value and the registration/override also has a validateValueCallback, WPF validates the default too. If the callback rejects the default value, ValidateDefaultValueCommon throws DefaultValueInvalid. The default must itself be a value the property would accept.

Solutions

  1. Change the metadata default to a value that passes the validator.
  2. Adjust the validator to accept the type default if it is a legitimate initial state.
  3. Use ValidateValueCallback only for invariants and CoerceValueCallback for business ranges.

Example fix

// before
new PropertyMetadata(0), v => (int)v > 0
// after
new PropertyMetadata(1), v => (int)v > 0
Defensive patterns

Strategy: validation

Validate before calling

if (validator != null && !validator(defaultValue)) throw new ArgumentException("Default value fails validator");

Prevention

When it happens

Trigger: Register(typeof(X), ..., new PropertyMetadata(0), v => (int)v > 0) — the default 0 fails the validator; similarly OverrideMetadata with a default that fails the inherited or supplied validator.

Common situations: Validators encoding business constraints (positive, non-empty, within range) combined with a naive type default like 0 or null.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs:434

                        valueAsISealable.Seal();

                        Invariant.Assert(dispatcherObject.Dispatcher == null,
                            "ISealable.Seal() failed after ISealable.CanSeal returned true");
                    }
                    else
                    {
                        throw new ArgumentException(SR.Format(SR.DefaultValueMustBeFreeThreaded, propertyName));
                    }
                }
            }


            // After checking for correct type, check default value against
            //  validator (when one is given)
            if ( validateValueCallback != null &&
                !validateValueCallback(defaultValue))
            {
                throw new ArgumentException(SR.Format(SR.DefaultValueInvalid, propertyName));
            }
        }


        /// <summary>
        ///     Parameter validation for OverrideMetadata, includes code to force
        /// all base classes of "forType" to register their metadata so we know
        /// what we are overriding.
        /// </summary>
        private void SetupOverrideMetadata(
                Type forType,
                PropertyMetadata typeMetadata,
            out DependencyObjectType dType,
            out PropertyMetadata baseMetadata )
        {
            ArgumentNullException.ThrowIfNull(forType);
            ArgumentNullException.ThrowIfNull(typeMetadata);

View on GitHub (pinned to 81131a70a4)