dotnet/wpf · error · InvalidOperationException
SR.FixedDocumentReadonly
Error message
SR.FixedDocumentReadonly
What it means
FixedTextPointer is a read-only pointer over a fixed document; inserting text into the text run at the pointer is not a supported mutation. InsertTextInRun unconditionally throws InvalidOperationException(SR.FixedDocumentReadonly) regardless of the supplied string (after a null check).
Solutions
- Do not attempt text edits on fixed documents; convert to an editable representation (e.g. FlowDocument) if editing is required
- Guard edits with a capability check on the container (fixed documents are immutable) before calling InsertTextInRun
- Catch InvalidOperationException and surface a read-only message to the user or disable editing UI
- Perform content changes at the document/package level (XPS package regeneration) instead of via ITextPointer
Example fix
// before
textPointer.InsertTextInRun("new text"); // always throws
// after
bool isReadOnly = textPointer is FixedTextPointer;
if (!isReadOnly) textPointer.InsertTextInRun("new text"); Defensive patterns
Strategy: validation
Validate before calling
if (pointer is System.Windows.Documents.FixedTextPointer)
throw new NotSupportedException("Fixed documents are read-only; cannot insert text."); Type guard
bool CanEdit(System.Windows.Documents.ITextPointer p) => p is not System.Windows.Documents.FixedTextPointer;
Prevention
- Check read-only status before any edit call
- Disable editing UI when viewing fixed documents
- Perform edits on FlowDocument copies, not fixed content
When it happens
Trigger: Calling ITextPointer.InsertTextInRun on any FixedTextPointer, at any position, with any non-null string.
Common situations: Generic text-editor logic (designed for RichTextBox/FlowDocument editing) invoked against a FixedDocument view, e.g. IME input, spell-check correction or programmatic text substitution routed through a TextNavigator over fixed content.
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
- SR.DocumentReadOnly
- 0x80040209
- args
- Cannot remove signature from read-only file.
- Cannot sign read-only file.
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/de8f223978ac24df.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Documents/FixedTextPointer.cs:495
/// </summary>
bool ITextPointer.MoveToNextInsertionPosition(LogicalDirection direction)
{
return TextPointerBase.MoveToNextInsertionPosition(this, direction);
}
//
// TextContainer modification methods. Disabled by design.
//
// This is readonly Text OM. All modification methods returns false
//
/// <summary>
/// </summary>
void ITextPointer.InsertTextInRun(string textData)
{
ArgumentNullException.ThrowIfNull(textData);
throw new InvalidOperationException(SR.FixedDocumentReadonly);
}
/// <summary>
/// <see cref="ITextPointer.DeleteContentToPosition"/>
/// </summary>
void ITextPointer.DeleteContentToPosition(ITextPointer limit)
{
throw new InvalidOperationException(SR.FixedDocumentReadonly);
}
/// <summary>
/// <see cref="ITextPointer.ValidateLayout"/>
/// </summary>
bool ITextPointer.ValidateLayout()
{
return TextPointerBase.ValidateLayout(this, ((ITextPointer)this).TextContainer.TextView);
}
View on GitHub (pinned to 81131a70a4)