dotnet/wpf · error · InvalidOperationException

SR.FlowDocumentInvalidContnetChange

Error message

SR.FlowDocumentInvalidContnetChange

What it means

FlowDocumentPaginator's page-metrics change handler detects an invalid content change: page dimensions changed while formatting (pagination) was in progress. It reports the violation via StructuralCache.OnInvalidOperationDetected() and throws InvalidOperationException (SR.FlowDocumentInvalidContnetChange). This is an internal consistency check.

Solutions

  1. Set page metrics only outside active pagination — before calling GetPage/ComputePageCount or after pagination completes.
  2. Defer metric changes with Dispatcher.BeginInvoke so they run after the current formatting pass.
  3. If writing a derived paginator, never mutate document page properties within overridden GetPage or pagination callbacks.

Example fix

// before
override void OnPageChanged(...) { flowDocument.PagePadding = newThickness; }
// after
void ApplyAfterPagination(Thickness t) { Dispatcher.BeginInvoke(() => flowDocument.PagePadding = t); }
Defensive patterns

Strategy: validation

Validate before calling

// Change page metrics only when no formatting is in progress:
if (!flowDocument.StructuralCache.IsFormattingInProgress)
    flowDocument.PagePadding = newThickness;
else
    Dispatcher.BeginInvoke(() => flowDocument.PagePadding = newThickness);

Try / catch

try { flowDocument.PageWidth = w; }
catch (InvalidOperationException)
{ Dispatcher.BeginInvoke(() => flowDocument.PageWidth = w); }

Prevention

When it happens

Trigger: Changing PageWidth/PageHeight/PagePadding (or other page metrics) on the FlowDocument from code that runs during pagination — e.g. inside GetPage, during a formatting callback, or from a handler invoked synchronously by layout.

Common situations: Custom paginators adjusting PagePadding inside GetPage overrides; reactive bindings that update page size while a print job is paginating; handlers triggered by the pagination pass itself updating page metrics.

Understand the failure class

Background: "This is a bug, please report it": internal invariant violations, unreachable panics, and SNH errors explained — this error's family across 47 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/FlowDocumentPaginator.cs:537

                Size newPageSize = value;
                if (double.IsNaN(newPageSize.Width))
                {
                    newPageSize.Width = _defaultPageSize.Width;
                }
                if (double.IsNaN(newPageSize.Height))
                {
                    newPageSize.Height = _defaultPageSize.Height;
                }
                Size oldActualSize = ComputePageSize();
                _pageSize = newPageSize;
                Size newActualSize = ComputePageSize();
                if (!DoubleUtil.AreClose(oldActualSize, newActualSize))
                {
                    // Detect invalid content change operations.
                    if (_document.StructuralCache.IsFormattingInProgress)
                    {
                        _document.StructuralCache.OnInvalidOperationDetected();
                        throw new InvalidOperationException(SR.FlowDocumentInvalidContnetChange);
                    }

                    // Any change of page metrics invalidates entire break record table.
                    // Hence page metrics change is treated in the same way as ContentChanged
                    // spanning entire content.
                    // NOTE: May execute external code, so it is possible to get
                    //       an exception here.
                    InvalidateBRT();
                }
            }
        }

        /// <summary>
        /// Whether content is paginated in the background.
        /// When True, the Paginator will paginate its content in the background,
        /// firing the PaginationCompleted and PaginationProgress events as appropriate.
        /// Background pagination begins immediately when set to True. If the
        /// PageSize is modified and this property is set to True, then all pages

View on GitHub (pinned to 81131a70a4)