dotnet/wpf · error · ArgumentException

SR.TextEditorPropertyIsNotApplicableForTextFormatting…

Error message

SR.TextEditorPropertyIsNotApplicableForTextFormatting (formattingProperty.Name)

What it means

TextRange.ApplyPropertyValue applies a formatting property to the range, but only character properties (e.g. FontSize, Foreground) and paragraph properties (e.g. ParagraphIndent) are supported. Passing any other DependencyProperty throws ArgumentException(SR.TextEditorPropertyIsNotApplicableForTextFormatting) naming the property.

Solutions

  1. Only pass properties where TextSchema.IsCharacterProperty or TextSchema.IsParagraphProperty returns true (e.g. TextElement.FontWeightProperty, Paragraph.TextIndentProperty).
  2. Filter the property with TextSchema checks before calling ApplyPropertyValue.
  3. Catch ArgumentException and route non-formatting properties to the appropriate element API instead.

Example fix

// before
selection.ApplyPropertyValue(FrameworkElement.WidthProperty, 100.0);
// after
if (TextSchema.IsCharacterProperty(prop) || TextSchema.IsParagraphProperty(prop))
    selection.ApplyPropertyValue(prop, value);
else
    ((TextElement)selection.Start.Parent).SetValue(prop, value);
Defensive patterns

Strategy: validation

Validate before calling

if (!TextSchema.IsCharacterProperty(prop) && !TextSchema.IsParagraphProperty(prop))
    throw new ArgumentException($"{prop.Name} is not a formatting property");

Type guard

static bool IsFormattingProperty(DependencyProperty p) => TextSchema.IsCharacterProperty(p) || TextSchema.IsParagraphProperty(p);

Try / catch

try { selection.ApplyPropertyValue(prop, value); }
catch (ArgumentException) { /* route to element-level SetValue or skip */ }

Prevention

When it happens

Trigger: Calling richTextBoxSelection.ApplyPropertyValue(someControl.DependencyProperty, value) with a property that is neither character nor paragraph property, e.g. FrameworkElement.WidthProperty, Grid.RowProperty, or a custom attached property.

Common situations: Reusing a generic property-setting helper on formatting ranges; passing layout properties expecting them to apply to selected text; applying custom attached properties to RichTextBox selections.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

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

        /// This parameter is used to resolve the ambiguity for overlapping inherited properties 
        /// that apply to both inline and paragraph elements.
        /// </param>
        /// <param name="propertyValueAction">
        /// Specifies how to apply the given value - use it for setting,
        /// for increasing or for decreasing existing values.
        /// This parameter must have PropertyValueAction.SetValue for all properties that
        /// cannot be incremented or decremented by their type.
        /// </param>
        internal void ApplyPropertyValue(DependencyProperty formattingProperty, object value, bool applyToParagraphs, PropertyValueAction propertyValueAction)
        {
            Invariant.Assert(this.HasConcreteTextContainer, "Can't apply property to non-TextContainer range!");

            ArgumentNullException.ThrowIfNull(formattingProperty);

            if (!TextSchema.IsCharacterProperty(formattingProperty) &&
                !TextSchema.IsParagraphProperty(formattingProperty))
            {
                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));
            }

View on GitHub (pinned to 81131a70a4)