{"record":{"id":"638af7b44f5b4790","repo":"dotnet/wpf","slug":"sr-format-sr-defaultvalueautoassignfailed-name-ownertype","errorCode":null,"errorMessage":"SR.Format(SR.DefaultValueAutoAssignFailed, name, ownerType.Name)","messagePattern":"SR\\.Format\\(SR\\.DefaultValueAutoAssignFailed, name, ownerType\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":343,"sourceCode":"\n            return defaultValue;\n        }\n\n        private static PropertyMetadata AutoGeneratePropertyMetadata(\n            Type propertyType,\n            ValidateValueCallback validateValueCallback,\n            string name,\n            Type ownerType)\n        {\n            // Default per-type metadata not provided, create\n            object defaultValue = AutoGenerateDefaultValue(propertyType);\n\n            // If a validator is passed in, see if the default value makes sense.\n            if ( validateValueCallback != null &&\n                !validateValueCallback(defaultValue))\n            {\n                // Didn't work - require the caller to specify one.\n                throw new ArgumentException(SR.Format(SR.DefaultValueAutoAssignFailed, name, ownerType.Name));\n            }\n\n            return new PropertyMetadata(defaultValue);\n        }\n\n        // Validate the default value in the given metadata\n        private static void ValidateMetadataDefaultValue(\n            PropertyMetadata defaultMetadata,\n            Type propertyType,\n            string propertyName,\n            ValidateValueCallback validateValueCallback )\n        {\n            // If we are registered to use the DefaultValue factory we can\n            // not validate the DefaultValue at registration time, so we\n            // early exit.\n            if (defaultMetadata.UsingDefaultValueFactory)\n            {\n                return;","sourceCodeStart":325,"sourceCodeEnd":361,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L325-L361","documentation":"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.","triggerScenarios":"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.","commonSituations":"Validators written to accept only business-valid values (positive numbers, non-empty strings) applied to properties registered without an explicit PropertyMetadata default.","solutions":["Pass a PropertyMetadata with a default value that satisfies the validateValueCallback.","Or relax the validator so it accepts the sensible type default (validators run on defaults too)."],"exampleFix":"// before\npublic static readonly DependencyProperty CountProperty =\n    DependencyProperty.Register(\"Count\", typeof(int), typeof(MyControl),\n        null, v => (int)v > 0);\n// after\npublic static readonly DependencyProperty CountProperty =\n    DependencyProperty.Register(\"Count\", typeof(int), typeof(MyControl),\n        new PropertyMetadata(1), v => (int)v > 0);","handlingStrategy":"validation","validationCode":"// Ensure the auto default (null / default(T)) passes before omitting metadata\nbool ok = validator(default(T));\nif (!ok) supplyExplicitMetadata = true;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Whenever a validateValueCallback rejects the type default, always pass explicit PropertyMetadata","Keep validators invariant-only so sensible defaults pass"],"tags":["wpf","dependency-property","default-value"],"backgroundTag":"invalid-argument-value","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}