{"record":{"id":"aa05b4e3c58a2ede","repo":"dotnet/wpf","slug":"sr-textrange","errorCode":null,"errorMessage":"SR.TextEditorTypeOfParameterIsNotAppropriateForFormattingProperty (value type, formattingProperty.Name)","messagePattern":"SR\\.TextEditorTypeOfParameterIsNotAppropriateForFormattingProperty \\(value type, formattingProperty\\.Name\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRange.cs","lineNumber":801,"sourceCode":"            {\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            }\n\n            // Check propertyValueAction validity\n            if (propertyValueAction != PropertyValueAction.SetValue &&\n                propertyValueAction != PropertyValueAction.IncreaseByAbsoluteValue &&\n                propertyValueAction != PropertyValueAction.DecreaseByAbsoluteValue &&\n                propertyValueAction != PropertyValueAction.IncreaseByPercentageValue &&\n                propertyValueAction != PropertyValueAction.DecreaseByPercentageValue)\n            {\n                throw new ArgumentException(SR.TextRange_InvalidParameterValue, nameof(propertyValueAction));\n            }\n            // Check if propertyValueAction is applicable to this property\n            if (propertyValueAction != PropertyValueAction.SetValue &&\n                !TextSchema.IsPropertyIncremental(formattingProperty))\n            {\n                throw new ArgumentException(SR.Format(SR.TextRange_PropertyCannotBeIncrementedOrDecremented, formattingProperty.Name), nameof(propertyValueAction));\n            }\n","sourceCodeStart":783,"sourceCodeEnd":819,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/TextRange.cs#L783-L819","documentation":"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\".","triggerScenarios":"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.","commonSituations":"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.","solutions":["Convert the value to the property's exact PropertyType first (e.g. convert string to double via TypeConverter or double.TryParse) before calling ApplyPropertyValue.","Check formattingProperty.IsValidValue(value) in the caller and handle invalid input there.","Catch ArgumentException on nameof(value) and surface a friendly message to the user."],"exampleFix":"// before\nselection.ApplyPropertyValue(TextElement.FontSizeProperty, \"14\");\n// after\nif (double.TryParse(input, out var size) && TextElement.FontSizeProperty.IsValidValue(size))\n    selection.ApplyPropertyValue(TextElement.FontSizeProperty, size);","handlingStrategy":"validation","validationCode":"if (value is string s && prop.PropertyType != typeof(string))\n    value = TypeDescriptor.GetConverter(prop.PropertyType).ConvertFromString(s);\nif (!prop.IsValidValue(value))\n    throw new ArgumentException($\"Invalid value for {prop.Name}\", nameof(value));","typeGuard":"static bool IsValidFormattingValue(DependencyProperty p, object v) => p.IsValidValue(v) || (p.PropertyType == typeof(Thickness) && v is Thickness);","tryCatchPattern":"try { selection.ApplyPropertyValue(prop, value); }\ncatch (ArgumentException ex) { ShowUserError($\"'{value}' is not valid for {prop.Name}\"); }","preventionTips":["Convert user input strings to the property's PropertyType before applying","Call IsValidValue in a pre-check","Guard numeric ranges (e.g. FontSize min/max) at input time"],"tags":["wpf","textrange","type-mismatch","formatting"],"backgroundTag":"type-mismatch","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"}