{"record":{"id":"e8f0fea019a702fb","repo":"dotnet/wpf","slug":"sr-format-sr-defaultvaluepropertytypemismatch-propertyname","errorCode":null,"errorMessage":"SR.Format(SR.DefaultValuePropertyTypeMismatch, propertyName)","messagePattern":"SR\\.Format\\(SR\\.DefaultValuePropertyTypeMismatch, propertyName\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs","lineNumber":386,"sourceCode":"        // Validate the given default value, used by PropertyMetadata.GetDefaultValue()\n        // when the DefaultValue factory is used.\n        // These default values are allowed to have thread-affinity.\n        internal void ValidateFactoryDefaultValue(object defaultValue)\n        {\n            ValidateDefaultValueCommon(defaultValue, PropertyType, Name, ValidateValueCallback, false);\n        }\n\n        private static void ValidateDefaultValueCommon(\n            object defaultValue,\n            Type propertyType,\n            string propertyName,\n            ValidateValueCallback validateValueCallback,\n            bool checkThreadAffinity)\n        {\n            // Ensure default value is the correct type\n            if (!IsValidType(defaultValue, propertyType))\n            {\n                throw new ArgumentException(SR.Format(SR.DefaultValuePropertyTypeMismatch, propertyName));\n            }\n\n            // An Expression used as default value won't behave as expected since\n            //  it doesn't get evaluated.  We explicitly fail it here.\n            if (defaultValue is Expression )\n            {\n                throw new ArgumentException(SR.DefaultValueMayNotBeExpression);\n            }\n\n            if (checkThreadAffinity)\n            {\n                // If the default value is a DispatcherObject with thread affinity\n                // we cannot accept it as a default value. If it implements ISealable\n                // we attempt to seal it; if not we throw  an exception. Types not\n                // deriving from DispatcherObject are allowed - it is up to the user to\n                // make any custom types free-threaded.\n\n","sourceCodeStart":368,"sourceCodeEnd":404,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/WindowsBase/System/Windows/DependencyProperty.cs#L368-L404","documentation":"ValidateDefaultValueCommon checks that the default value supplied in PropertyMetadata is assignment-compatible with the property's type via IsValidType. If the default's runtime type does not match (or is not a derived type of) propertyType, registration fails. This protects the property system from handing out invalid defaults.","triggerScenarios":"new PropertyMetadata(\"hello\") on a property registered as typeof(int); a base-type default (e.g. object) for a specific typed property; a null-typed mismatch after refactoring the property type without updating metadata.","commonSituations":"Changing propertyType during a refactor while leaving the old default in PropertyMetadata; copy-pasted metadata across differently-typed properties; boxed value types of the wrong kind (double 0.0 vs int).","solutions":["Make the default value exactly the property's type (or a derived type).","For numeric properties, match the literal type (use 0.0 for double, 0 for int).","After changing a property's type, update its PropertyMetadata default accordingly."],"exampleFix":"// before\npublic static readonly DependencyProperty SizeProperty =\n    DependencyProperty.Register(\"Size\", typeof(double), typeof(MyControl),\n        new PropertyMetadata(0));\n// after\npublic static readonly DependencyProperty SizeProperty =\n    DependencyProperty.Register(\"Size\", typeof(double), typeof(MyControl),\n        new PropertyMetadata(0.0));","handlingStrategy":"type-guard","validationCode":"if (defaultValue != null && !propertyType.IsInstanceOfType(defaultValue)) throw new ArgumentException(\"Default value type mismatch\");","typeGuard":"static bool IsValidDefault(object defaultValue, Type propertyType) => defaultValue == null || propertyType.IsInstanceOfType(defaultValue);","tryCatchPattern":null,"preventionTips":["Use correctly-typed literals in PropertyMetadata (0.0 for double, 0 for int)","Re-check metadata defaults whenever propertyType changes","Unit-test DP registration in a static initializer test"],"tags":["wpf","dependency-property","type-mismatch"],"backgroundTag":"type-mismatch","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"}