{"record":{"id":"21547186d529d613","repo":"dotnet/wpf","slug":"sr-defaultvaluemaynotbeunset","errorCode":null,"errorMessage":"SR.DefaultValueMayNotBeUnset","messagePattern":"SR\\.DefaultValueMayNotBeUnset","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/PropertyMetadata.cs","lineNumber":97,"sourceCode":"                {\n                    return _defaultValue;\n                }\n                else\n                {\n                    return defaultFactory.DefaultValue;\n                }\n            }\n\n            set\n            {\n                if (Sealed)\n                {\n                    throw new InvalidOperationException(SR.TypeMetadataCannotChangeAfterUse);\n                }\n\n                if (value == DependencyProperty.UnsetValue)\n                {\n                    throw new ArgumentException(SR.DefaultValueMayNotBeUnset);\n                }\n\n                _defaultValue = value;\n\n                SetModified(MetadataFlags.DefaultValueModifiedID);\n            }\n        }\n\n\n        /// <summary>\n        ///     Returns true if the default value is a DefaultValueFactory\n        /// </summary>\n        internal bool UsingDefaultValueFactory\n        {\n            get\n            {\n                return _defaultValue is DefaultValueFactory;\n            }","sourceCodeStart":79,"sourceCodeEnd":115,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/PropertyMetadata.cs#L79-L115","documentation":"PropertyMetadata.DefaultValue rejects DependencyProperty.UnsetValue: assigning it throws ArgumentException with DefaultValueMayNotBeUnset. UnsetValue is the property system's internal sentinel for 'no value', so it is not a legal default value for metadata.","triggerScenarios":"Setting metadata.DefaultValue = DependencyProperty.UnsetValue (or constructing PropertyMetadata with DependencyProperty.UnsetValue as its defaultValue argument), often intending to 'clear' a default.","commonSituations":"Trying to reset a default to 'unset' during OverrideMetadata; generic helper code that passes a value straight from a lookup which returned UnsetValue; confusion between UnsetValue and null as a default.","solutions":["Assign null (for reference types) or a valid typed default instead of DependencyProperty.UnsetValue.","If the goal is to inherit the base default, do not set DefaultValue at all — omit it in OverrideMetadata so Merge keeps the base value.","Guard helper code: skip or translate UnsetValue before passing into PropertyMetadata."],"exampleFix":"// before\nvar meta = new PropertyMetadata(DependencyProperty.UnsetValue); // throws\n\n// after\nvar meta = new PropertyMetadata(null); // or omit default to inherit base metadata default","handlingStrategy":"validation","validationCode":"if (candidate == DependencyProperty.UnsetValue)\n    candidate = null; // or skip setting a default entirely to inherit the base default\nvar meta = new PropertyMetadata(candidate);","typeGuard":"static bool IsValidDefaultValue(object v) => v != DependencyProperty.UnsetValue;","tryCatchPattern":"try\n{\n    metadata.DefaultValue = value;\n}\ncatch (ArgumentException ex) when (ex.Message.Contains(\"DefaultValue\"))\n{\n    // UnsetValue supplied; choose null or omit to inherit base default\n}","preventionTips":["Never use DependencyProperty.UnsetValue as a user-visible default; it is a property-system sentinel.","Omit the default in OverrideMetadata when you want to inherit the base metadata's default."],"tags":["wpf","dependency-properties","metadata","unsetvalue"],"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-21T21:30:21.729Z"}