dotnet/wpf · error · ArgumentException

SR.TextRange_InvalidParameterValue

Error message

SR.TextRange_InvalidParameterValue

What it means

TextRange.ApplyPropertyValue throws ArgumentException when the propertyValueAction parameter is not one of the five recognized PropertyValueAction values (SetValue, IncreaseByAbsoluteValue, DecreaseByAbsoluteValue, IncreaseByPercentageValue, DecreaseByPercentageValue). The library validates the action enum explicitly to prevent undefined behavior in property application. This is a caller-supplied parameter validation guard.

Solutions

  1. Use only the defined PropertyValueAction members; for plain assignment use PropertyValueAction.SetValue
  2. Check Enum.IsDefined(typeof(PropertyValueAction), action) before calling ApplyPropertyValue
  3. If intending to increment/decrement, confirm the property supports it (see TextSchema.IsPropertyIncremental) and use the Increase/Decrease actions

Example fix

// before
range.ApplyPropertyValue(FontSizeProperty, 20.0, (PropertyValueAction)7);
// after
range.ApplyPropertyValue(FontSizeProperty, 20.0, PropertyValueAction.SetValue);
Defensive patterns

Strategy: validation

Validate before calling

if (!Enum.IsDefined(typeof(PropertyValueAction), action)) throw new ArgumentOutOfRangeException(nameof(action));

Type guard

bool IsValidPropertyValueAction(PropertyValueAction a) => a is PropertyValueAction.SetValue or PropertyValueAction.IncreaseByAbsoluteValue or PropertyValueAction.DecreaseByAbsoluteValue or PropertyValueAction.IncreaseByPercentageValue or PropertyValueAction.DecreaseByPercentageValue;

Try / catch

try { range.ApplyPropertyValue(prop, value, action); } catch (ArgumentException ex) { /* invalid PropertyValueAction */ }

Prevention

When it happens

Trigger: Calling TextRange.ApplyPropertyValue (or TextSelection.ApplyPropertyValue overloads that accept a PropertyValueAction) with an out-of-range cast value like (PropertyValueAction)99, or a default-initialized struct that was never assigned a valid enum member.

Common situations: Reflection-based formatting code casting ints to PropertyValueAction; deserialized or persisted enum values from older WPF versions; copy-pasted code inventing new action names.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

            }

            // 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));
            }

            ApplyPropertyToTextVirtual(formattingProperty, value, applyToParagraphs, propertyValueAction);
        }

        /// <summary>
        /// Removes all Inline formatting properties from this range.
        /// Affects only Inline elements: splits the on range borders
        /// and deletes all Inlines inside the range.
        /// Properties set on Paragraphs and other enclosing Block elements
        /// remain intact.
        /// </summary>

View on GitHub (pinned to 81131a70a4)