{"record":{"id":"43c3b0311a07a2aa","repo":"dotnet/wpf","slug":"sr-format-sr-invalidsettervalue-value-dp-ownertype-dp-name","errorCode":null,"errorMessage":"SR.Format(SR.InvalidSetterValue, value, dp.OwnerType, dp.Name)","messagePattern":"SR\\.Format\\(SR\\.InvalidSetterValue, value, dp\\.OwnerType, dp\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs","lineNumber":119,"sourceCode":"\n            // Value needs to be valid for the DP, or a deferred reference, or one of the supported\n            // markup extensions.\n\n            if (!dp.IsValidValue(value))\n            {\n                // The only markup extensions supported by styles is resources and bindings.\n                if (value is MarkupExtension)\n                {\n                    if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )\n                    {\n                        throw new ArgumentException(SR.Format(SR.SetterValueOfMarkupExtensionNotSupported,\n                                                           value.GetType().Name));\n                    }\n                }\n\n                else if (!(value is DeferredReference))\n                {\n                    throw new ArgumentException(SR.Format(SR.InvalidSetterValue, value, dp.OwnerType, dp.Name));\n                }\n            }\n\n            // Freeze the value for the setter\n            StyleHelper.SealIfSealable(_value);\n\n            base.Seal();\n        }\n\n        /// <summary>\n        ///    Property that is being set by this setter\n        /// </summary>\n        [Ambient]\n        [DefaultValue(null)]\n        [Localizability(LocalizationCategory.None, Modifiability = Modifiability.Unmodifiable, Readability = Readability.Unreadable)] // Not localizable by-default\n        public DependencyProperty Property\n        {\n            get { return _property; }","sourceCodeStart":101,"sourceCodeEnd":137,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs#L101-L137","documentation":"During Seal(), the Setter's value must be valid for the target dependency property, be a DeferredReference, or be a supported markup extension. WPF throws this ArgumentException when none of those hold — typically when the value's type does not match what the dependency property expects and the value survived to the sealing stage.","triggerScenarios":"Assigning a value of the wrong type to a Setter in code (e.g. a string where a Brush is required) where the value was not a MarkupExtension or DeferredReference; the message names the offending value, the owner type, and the property.","commonSituations":"Code-built styles assigning raw strings or wrong-typed objects; reflection-generated UI where XAML's type converters never ran; deserialized settings applied directly as setter values.","solutions":["Convert the value to the property's expected type before assigning (e.g. use a Brush, Thickness, or the correct enum).","Use the dependency property's type from dp.PropertyType and run an appropriate TypeConverter (e.g. new BrushConverter().ConvertFromString(...)).","If the value is dynamic, wrap it in a Binding or DynamicResourceExtension so the runtime converter handles it."],"exampleFix":"// before\nsetter.Value = \"Red\"; // string assigned to BackgroundProperty\n// after\nsetter.Value = new BrushConverter().ConvertFromString(\"Red\");","handlingStrategy":"type-guard","validationCode":"if (value != null && dp != null && !dp.PropertyType.IsInstanceOfType(value))\n    value = TypeDescriptor.GetConverter(dp.PropertyType)?.ConvertFrom(value) ?? value;","typeGuard":"bool IsValidFor(Setter s) => s.Property == null || s.Value == null || s.Property.PropertyType.IsInstanceOfType(s.Value);","tryCatchPattern":"try { element.Style = style; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Invalid setter value\")) { /* convert value types */ }","preventionTips":["Convert strings/values to the dependency property's PropertyType before assigning","Use type converters (BrushConverter, ThicknessConverter) for XAML-like string inputs"],"tags":["wpf","style","type-mismatch","setter"],"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"}