{"record":{"id":"096f34ecfcdd2e14","repo":"dotnet/wpf","slug":"sr-texteditorpropertyisnotapplicablefortextformatting","errorCode":null,"errorMessage":"SR.TextEditorPropertyIsNotApplicableForTextFormatting (formattingProperty.Name)","messagePattern":"SR\\.TextEditorPropertyIsNotApplicableForTextFormatting \\(formattingProperty\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRange.cs","lineNumber":784,"sourceCode":"        /// This parameter is used to resolve the ambiguity for overlapping inherited properties \n        /// that apply to both inline and paragraph elements.\n        /// </param>\n        /// <param name=\"propertyValueAction\">\n        /// Specifies how to apply the given value - use it for setting,\n        /// for increasing or for decreasing existing values.\n        /// This parameter must have PropertyValueAction.SetValue for all properties that\n        /// cannot be incremented or decremented by their type.\n        /// </param>\n        internal void ApplyPropertyValue(DependencyProperty formattingProperty, object value, bool applyToParagraphs, PropertyValueAction propertyValueAction)\n        {\n            Invariant.Assert(this.HasConcreteTextContainer, \"Can't apply property to non-TextContainer range!\");\n\n            ArgumentNullException.ThrowIfNull(formattingProperty);\n\n            if (!TextSchema.IsCharacterProperty(formattingProperty) &&\n                !TextSchema.IsParagraphProperty(formattingProperty))\n            {\n                throw new ArgumentException(SR.Format(SR.TextEditorPropertyIsNotApplicableForTextFormatting, formattingProperty.Name));\n            }\n\n            // Convert property value from a string to object if needed\n            if ((value is string) && formattingProperty.PropertyType != typeof(string))\n            {\n                System.ComponentModel.TypeConverter typeConverter = System.ComponentModel.TypeDescriptor.GetConverter(formattingProperty.PropertyType);\n                Invariant.Assert(typeConverter != null);\n                value = typeConverter.ConvertFromString((string)value);\n            }\n\n            // Check if the value is appropriate for the property\n            if (!formattingProperty.IsValidValue(value) &&\n                !(formattingProperty.PropertyType == typeof(Thickness) && (value is Thickness)))\n            {\n                // We exclude checking thcickness values because we have special treatment for negative values\n                // in TextRangeEdit.SetParagraphProperty - negative values mean: \"leave the value as is\".\n                throw new ArgumentException(SR.Format(SR.TextEditorTypeOfParameterIsNotAppropriateForFormattingProperty, value == null ? \"null\" : value.GetType().Name, formattingProperty.Name), nameof(value));\n            }","sourceCodeStart":766,"sourceCodeEnd":802,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRange.cs#L766-L802","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Only pass properties where TextSchema.IsCharacterProperty or TextSchema.IsParagraphProperty returns true (e.g. TextElement.FontWeightProperty, Paragraph.TextIndentProperty).","Filter the property with TextSchema checks before calling ApplyPropertyValue.","Catch ArgumentException and route non-formatting properties to the appropriate element API instead."],"exampleFix":"// before\nselection.ApplyPropertyValue(FrameworkElement.WidthProperty, 100.0);\n// after\nif (TextSchema.IsCharacterProperty(prop) || TextSchema.IsParagraphProperty(prop))\n    selection.ApplyPropertyValue(prop, value);\nelse\n    ((TextElement)selection.Start.Parent).SetValue(prop, value);","handlingStrategy":"validation","validationCode":"if (!TextSchema.IsCharacterProperty(prop) && !TextSchema.IsParagraphProperty(prop))\n    throw new ArgumentException($\"{prop.Name} is not a formatting property\");","typeGuard":"static bool IsFormattingProperty(DependencyProperty p) => TextSchema.IsCharacterProperty(p) || TextSchema.IsParagraphProperty(p);","tryCatchPattern":"try { selection.ApplyPropertyValue(prop, value); }\ncatch (ArgumentException) { /* route to element-level SetValue or skip */ }","preventionTips":["Whitelist only character/paragraph properties for formatting ranges","Apply layout properties to elements directly, not selections","Test property-application helpers with non-formatting properties"],"tags":["wpf","textrange","formatting","dependency-property"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}