dotnet/wpf · error · ArgumentException

SR.Format(SR.InvalidPropertyValue, value, dp.Name)

Error message

SR.Format(SR.InvalidPropertyValue, value, dp.Name)

What it means

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.

Solutions

  1. Convert the value to the type the DP expects (e.g. double.Parse, new Thickness(...), Brushes.X) before calling SetValue.
  2. If the value comes from a string, use TypeConverter/Convert.ChangeType based on dp.PropertyType before SetValue.
  3. 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.
  4. Check dp.ValidateValueCallback / property metadata to understand what the property rejects.

Example fix

// before
factory.SetValue(Button.PaddingProperty, "10");
// after
factory.SetValue(Button.PaddingProperty, new Thickness(10));
Defensive patterns

Strategy: validation

Validate before calling

if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))
    throw new ArgumentException($"{value} is not valid for {dp.Name}");

Type guard

static bool IsValidForDp(DependencyProperty dp, object value) =>
    value is MarkupExtension or DeferredReference || dp.IsValidValue(value);

Try / catch

try { factory.SetValue(dp, value); }
catch (ArgumentException ex) { log.LogWarning($"Rejected value for {dp.Name}: {ex.Message}"); }

Prevention

When it happens

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

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

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/FrameworkElementFactory.cs:209

        ///     Simple value set on template child
        /// </summary>
        /// <param name="dp">Dependent property</param>
        /// <param name="value">Value to set</param>
        public void SetValue(DependencyProperty dp, object value)
        {
            if (_sealed)
            {
                throw new InvalidOperationException(SR.Format(SR.CannotChangeAfterSealed, "FrameworkElementFactory"));
            }

            ArgumentNullException.ThrowIfNull(dp);

            // Value needs to be valid for the DP, or Binding/MultiBinding/PriorityBinding.
            //  (They all have MarkupExtension, which we don't actually support, see above check.)

            if (!dp.IsValidValue(value) && !(value is MarkupExtension) && !(value is DeferredReference))
            {
                throw new ArgumentException(SR.Format(SR.InvalidPropertyValue, value, dp.Name));
            }

            // Styling the logical tree is not supported
            if (StyleHelper.IsStylingLogicalTree(dp, value))
            {
                throw new NotSupportedException(SR.Format(SR.ModifyingLogicalTreeViaStylesNotImplemented, value, "FrameworkElementFactory.SetValue"));
            }

            if (dp.ReadOnly)
            {
                // Read-only properties will not be consulting FrameworkElementFactory for value.
                //  Rather than silently do nothing, throw error.
                throw new ArgumentException(SR.Format(SR.ReadOnlyPropertyNotAllowed, dp.Name, GetType().Name));
            }

            ResourceReferenceExpression resourceExpression = value as ResourceReferenceExpression;
            DynamicResourceExtension dynamicResourceExtension = value as DynamicResourceExtension;
            object resourceKey = null;

View on GitHub (pinned to 81131a70a4)