dotnet/wpf · error · ArgumentException

SR.Format(SR.DefaultValueAutoAssignFailed, name…

Error message

SR.Format(SR.DefaultValueAutoAssignFailed, name, ownerType.Name)

What it means

When Register* is called without metadata, WPF auto-generates a default value (null, or type default). If a validateValueCallback was supplied and it rejects that auto-generated default, AutoGeneratePropertyMetadata throws, demanding the caller supply an explicit default value. The library cannot pick a default that passes your validator, so you must provide one.

Solutions

  1. Pass a PropertyMetadata with a default value that satisfies the validateValueCallback.
  2. Or relax the validator so it accepts the sensible type default (validators run on defaults too).

Example fix

// before
public static readonly DependencyProperty CountProperty =
    DependencyProperty.Register("Count", typeof(int), typeof(MyControl),
        null, v => (int)v > 0);
// after
public static readonly DependencyProperty CountProperty =
    DependencyProperty.Register("Count", typeof(int), typeof(MyControl),
        new PropertyMetadata(1), v => (int)v > 0);
Defensive patterns

Strategy: validation

Validate before calling

// Ensure the auto default (null / default(T)) passes before omitting metadata
bool ok = validator(default(T));
if (!ok) supplyExplicitMetadata = true;

Prevention

When it happens

Trigger: Register with a validateValueCallback whose logic rejects the type default (e.g. validator requires non-null or value >= 1 while the auto default is null/0) and no defaultMetadata/defaultValue passed.

Common situations: Validators written to accept only business-valid values (positive numbers, non-empty strings) applied to properties registered without an explicit PropertyMetadata default.

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

Appendix: source

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

            return defaultValue;
        }

        private static PropertyMetadata AutoGeneratePropertyMetadata(
            Type propertyType,
            ValidateValueCallback validateValueCallback,
            string name,
            Type ownerType)
        {
            // Default per-type metadata not provided, create
            object defaultValue = AutoGenerateDefaultValue(propertyType);

            // If a validator is passed in, see if the default value makes sense.
            if ( validateValueCallback != null &&
                !validateValueCallback(defaultValue))
            {
                // Didn't work - require the caller to specify one.
                throw new ArgumentException(SR.Format(SR.DefaultValueAutoAssignFailed, name, ownerType.Name));
            }

            return new PropertyMetadata(defaultValue);
        }

        // Validate the default value in the given metadata
        private static void ValidateMetadataDefaultValue(
            PropertyMetadata defaultMetadata,
            Type propertyType,
            string propertyName,
            ValidateValueCallback validateValueCallback )
        {
            // If we are registered to use the DefaultValue factory we can
            // not validate the DefaultValue at registration time, so we
            // early exit.
            if (defaultMetadata.UsingDefaultValueFactory)
            {
                return;

View on GitHub (pinned to 81131a70a4)