dotnet/wpf · error · ArgumentException

SR.TextEditorTypeOfParameterIsNotAppropriateForFormattingPro…

Error message

SR.TextEditorTypeOfParameterIsNotAppropriateForFormattingProperty (value type, formattingProperty.Name)

What it means

After confirming the property is a formatting property, TextRange.ApplyPropertyValue validates that the supplied value is acceptable via formattingProperty.IsValidValue(value), with a special exclusion for Thickness values. When the value's type (or the value itself) is not valid for the property, it throws ArgumentException(SR.TextEditorTypeOfParameterIsNotAppropriateForFormattingProperty) reporting the value's type name and property name, with paramName "value".

Solutions

  1. Convert the value to the property's exact PropertyType first (e.g. convert string to double via TypeConverter or double.TryParse) before calling ApplyPropertyValue.
  2. Check formattingProperty.IsValidValue(value) in the caller and handle invalid input there.
  3. Catch ArgumentException on nameof(value) and surface a friendly message to the user.

Example fix

// before
selection.ApplyPropertyValue(TextElement.FontSizeProperty, "14");
// after
if (double.TryParse(input, out var size) && TextElement.FontSizeProperty.IsValidValue(size))
    selection.ApplyPropertyValue(TextElement.FontSizeProperty, size);
Defensive patterns

Strategy: validation

Validate before calling

if (value is string s && prop.PropertyType != typeof(string))
    value = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString(s);
if (!prop.IsValidValue(value))
    throw new ArgumentException($"Invalid value for {prop.Name}", nameof(value));

Type guard

static bool IsValidFormattingValue(DependencyProperty p, object v) => p.IsValidValue(v) || (p.PropertyType == typeof(Thickness) && v is Thickness);

Try / catch

try { selection.ApplyPropertyValue(prop, value); }
catch (ArgumentException ex) { ShowUserError($"'{value}' is not valid for {prop.Name}"); }

Prevention

When it happens

Trigger: Calling ApplyPropertyValue(TextElement.FontSizeProperty, "big") with a string/incorrectly-typed value that cannot be converted or validated, e.g. FontSize with a non-numeric or negative value, FontWeight with a non-FontWeight object, or null for a non-nullable property.

Common situations: Passing raw strings from user input without conversion when TypeConverter fails; passing FontSizes outside the valid range; passing values of the wrong type after a refactor; deserialized settings with wrong types.

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/aa05b4e3c58a2ede. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRange.cs:801

            {
                throw new ArgumentException(SR.Format(SR.TextEditorPropertyIsNotApplicableForTextFormatting, formattingProperty.Name));
            }

            // Convert property value from a string to object if needed
            if ((value is string) && formattingProperty.PropertyType != typeof(string))
            {
                System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(formattingProperty.PropertyType);
                Invariant.Assert(typeConverter != null);
                value = typeConverter.ConvertFromString((string)value);
            }

            // Check if the value is appropriate for the property
            if (!formattingProperty.IsValidValue(value) &&
                !(formattingProperty.PropertyType == typeof(Thickness) && (value is Thickness)))
            {
                // We exclude checking thcickness values because we have special treatment for negative values
                // in TextRangeEdit.SetParagraphProperty - negative values mean: "leave the value as is".
                throw new ArgumentException(SR.Format(SR.TextEditorTypeOfParameterIsNotAppropriateForFormattingProperty, value == null ? "null" : value.GetType().Name, formattingProperty.Name), nameof(value));
            }

            // Check propertyValueAction validity
            if (propertyValueAction != PropertyValueAction.SetValue &&
                propertyValueAction != PropertyValueAction.IncreaseByAbsoluteValue &&
                propertyValueAction != PropertyValueAction.DecreaseByAbsoluteValue &&
                propertyValueAction != PropertyValueAction.IncreaseByPercentageValue &&
                propertyValueAction != PropertyValueAction.DecreaseByPercentageValue)
            {
                throw new ArgumentException(SR.TextRange_InvalidParameterValue, nameof(propertyValueAction));
            }
            // Check if propertyValueAction is applicable to this property
            if (propertyValueAction != PropertyValueAction.SetValue &&
                !TextSchema.IsPropertyIncremental(formattingProperty))
            {
                throw new ArgumentException(SR.Format(SR.TextRange_PropertyCannotBeIncrementedOrDecremented, formattingProperty.Name), nameof(propertyValueAction));
            }

View on GitHub (pinned to 81131a70a4)