dotnet/wpf · error · ArgumentException

SR.TextRange_PropertyCannotBeIncrementedOrDecremented…

Error message

SR.TextRange_PropertyCannotBeIncrementedOrDecremented (formattingProperty.Name)

What it means

TextRange.ApplyPropertyValue throws ArgumentException when propertyValueAction is an increment/decrement action (IncreaseByAbsoluteValue, DecreaseByAbsoluteValue, IncreaseByPercentageValue, DecreaseByPercentageValue) but the target property is not incremental per TextSchema.IsPropertyIncremental. Only properties like FontSize that support arithmetic adjustment can use these actions; others (e.g. Foreground) require SetValue. The message includes the offending property name.

Solutions

  1. Use PropertyValueAction.SetValue for non-incremental properties
  2. Only use Increase/Decrease actions for incremental properties (e.g. FontSize)
  3. Guard with TextSchema.IsPropertyIncremental(property) before choosing an increment action

Example fix

// before
range.ApplyPropertyValue(ForegroundProperty, Brushes.Red, PropertyValueAction.IncreaseByAbsoluteValue);
// after
range.ApplyPropertyValue(ForegroundProperty, Brushes.Red, PropertyValueAction.SetValue);
Defensive patterns

Strategy: validation

Validate before calling

if (action != PropertyValueAction.SetValue && !TextSchema.IsPropertyIncremental(property)) action = PropertyValueAction.SetValue;

Type guard

bool CanIncrement(DependencyProperty p) => TextSchema.IsPropertyIncremental(p);

Try / catch

try { range.ApplyPropertyValue(prop, value, action); } catch (ArgumentException ex) { /* property not incremental */ action = PropertyValueAction.SetValue; }

Prevention

When it happens

Trigger: Calling ApplyPropertyValue with e.g. PropertyValueAction.IncreaseByAbsoluteValue on a non-incremental property such as Foreground, FontStyle, or FontWeight.

Common situations: Building toolbar +/- font-size buttons with generic code paths applied to all formatting properties; loop-based formatting code that reuses the same action for every property.

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

Appendix: source

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

                // 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>
        public void ClearAllProperties()
        {
            Invariant.Assert(this.HasConcreteTextContainer, "Can't clear properties in non-TextContainer range");

            ClearAllPropertiesVirtual();
        }

View on GitHub (pinned to 81131a70a4)