{"record":{"id":"e3c3c8665e5ebe24","repo":"dotnet/wpf","slug":"sr-format-sr-invalidpropertyvalue-value-dp-name","errorCode":null,"errorMessage":"SR.Format(SR.InvalidPropertyValue, value, dp.Name)","messagePattern":"SR\\.Format\\(SR\\.InvalidPropertyValue, value, dp\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs","lineNumber":209,"sourceCode":"        ///     Simple value set on template child\n        /// </summary>\n        /// <param name=\"dp\">Dependent property</param>\n        /// <param name=\"value\">Value to set</param>\n        public void SetValue(DependencyProperty dp, object value)\n        {\n            if (_sealed)\n            {\n                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, \"FrameworkElementFactory\"));\n            }\n\n            ArgumentNullException.ThrowIfNull(dp);\n\n            // Value needs to be valid for the DP, or Binding/MultiBinding/PriorityBinding.\n            //  (They all have MarkupExtension, which we don't actually support, see above check.)\n\n            if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidPropertyValue, value, dp.Name));\n            }\n\n            // Styling the logical tree is not supported\n            if (StyleHelper.IsStylingLogicalTree(dp, value))\n            {\n                throw new NotSupportedException(SR.Format(SR.ModifyingLogicalTreeViaStylesNotImplemented, value, \"FrameworkElementFactory.SetValue\"));\n            }\n\n            if (dp.ReadOnly)\n            {\n                // Read-only properties will not be consulting FrameworkElementFactory for value.\n                //  Rather than silently do nothing, throw error.\n                throw new ArgumentException(SR.Format(SR.ReadOnlyPropertyNotAllowed, dp.Name, GetType().Name));\n            }\n\n            ResourceReferenceExpression resourceExpression = value as ResourceReferenceExpression;\n            DynamicResourceExtension dynamicResourceExtension = value as DynamicResourceExtension;\n            object resourceKey = null;","sourceCodeStart":191,"sourceCodeEnd":227,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs#L191-L227","documentation":"FrameworkElementFactory.SetValue throws ArgumentException when the value is not valid for the target dependency property (dp.IsValidValue fails) and is also not a MarkupExtension or DeferredReference. The factory validates values eagerly so that invalid values fail at template-authoring time instead of when the template is instantiated. This is a programming error: the supplied value's type does not match the property's registered type or violates its validation callback.","triggerScenarios":"Calling factory.SetValue(SomeProperty, value) where value fails DependencyProperty.IsValidValue — e.g. assigning a string to a Double property, a negative number to a property with a validation callback, or an object of the wrong type.","commonSituations":"Hand-building templates in code (new FrameworkElementFactory(typeof(Button))); porting XAML to code-behind where type conversion the XAML parser did for free no longer happens; binding values boxed as strings from config files.","solutions":["Convert the value to the type the DP expects (e.g. double.Parse, new Thickness(...), Brushes.X) before calling SetValue.","If the value comes from a string, use TypeConverter/Convert.ChangeType based on dp.PropertyType before SetValue.","If the value should be computed per-instance, use a Binding (with a source) or set the property on the instantiated element instead of the factory.","Check dp.ValidateValueCallback / property metadata to understand what the property rejects."],"exampleFix":"// before\nfactory.SetValue(Button.PaddingProperty, \"10\");\n// after\nfactory.SetValue(Button.PaddingProperty, new Thickness(10));","handlingStrategy":"validation","validationCode":"if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))\n    throw new ArgumentException($\"{value} is not valid for {dp.Name}\");","typeGuard":"static bool IsValidForDp(DependencyProperty dp, object value) =>\n    value is MarkupExtension or DeferredReference || dp.IsValidValue(value);","tryCatchPattern":"try { factory.SetValue(dp, value); }\ncatch (ArgumentException ex) { log.LogWarning($\"Rejected value for {dp.Name}: {ex.Message}\"); }","preventionTips":["Check dp.PropertyType and convert values before SetValue.","Prefer strongly typed literals over strings parsed at runtime.","Consult the DP's ValidateValueCallback in metadata when unsure."],"tags":["wpf","dependency-property","argument-exception"],"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"}