dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidSetterValue, value, dp.OwnerType…

Error message

SR.Format(SR.InvalidSetterValue, value, dp.OwnerType, dp.Name)

What it means

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.

Solutions

  1. Convert the value to the property's expected type before assigning (e.g. use a Brush, Thickness, or the correct enum).
  2. Use the dependency property's type from dp.PropertyType and run an appropriate TypeConverter (e.g. new BrushConverter().ConvertFromString(...)).
  3. If the value is dynamic, wrap it in a Binding or DynamicResourceExtension so the runtime converter handles it.

Example fix

// before
setter.Value = "Red"; // string assigned to BackgroundProperty
// after
setter.Value = new BrushConverter().ConvertFromString("Red");
Defensive patterns

Strategy: type-guard

Validate before calling

if (value != null && dp != null && !dp.PropertyType.IsInstanceOfType(value))
    value = TypeDescriptor.GetConverter(dp.PropertyType)?.ConvertFrom(value) ?? value;

Type guard

bool IsValidFor(Setter s) => s.Property == null || s.Value == null || s.Property.PropertyType.IsInstanceOfType(s.Value);

Try / catch

try { element.Style = style; }
catch (ArgumentException ex) when (ex.Message.Contains("Invalid setter value")) { /* convert value types */ }

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/43c3b0311a07a2aa. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Setter.cs:119

            // Value needs to be valid for the DP, or a deferred reference, or one of the supported
            // markup extensions.

            if (!dp.IsValidValue(value))
            {
                // The only markup extensions supported by styles is resources and bindings.
                if (value is MarkupExtension)
                {
                    if ( !(value is DynamicResourceExtension) && !(value is System.Windows.Data.BindingBase) )
                    {
                        throw new ArgumentException(SR.Format(SR.SetterValueOfMarkupExtensionNotSupported,
                                                           value.GetType().Name));
                    }
                }

                else if (!(value is DeferredReference))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidSetterValue, value, dp.OwnerType, dp.Name));
                }
            }

            // Freeze the value for the setter
            StyleHelper.SealIfSealable(_value);

            base.Seal();
        }

        /// <summary>
        ///    Property that is being set by this setter
        /// </summary>
        [Ambient]
        [DefaultValue(null)]
        [Localizability(LocalizationCategory.None, Modifiability = Modifiability.Unmodifiable, Readability = Readability.Unreadable)] // Not localizable by-default
        public DependencyProperty Property
        {
            get { return _property; }

View on GitHub (pinned to 81131a70a4)