dotnet/wpf · error

SR.DocumentGridVisualTreeOutOfSync

Error message

SR.DocumentGridVisualTreeOutOfSync

What it means

InvalidOperationException from DocumentGrid.ArrangeOverride: the row cache and the visual tree have diverged (e.g. the row layout was recomputed between Measure and Arrange, or the visible-row bookkeeping no longer matches the child visuals). The throw is an internal consistency check during page arrangement.

Solutions

  1. Avoid changing pagination, scale, or content between the measure and arrange passes (e.g. in ArrangeOverride overrides or bindings)
  2. Ensure InvalidateMeasure is called rather than mutating the row cache directly when document state changes
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs:1189 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs:1189

            //1 which is our first page.
            int visualChild = _firstPageVisualIndex;

            //Now walk through the visible rows and arrange the pages therein.
            for (int row = _firstVisibleRow; row < _firstVisibleRow + _visibleRowCount; row++)
            {
                //Calculate the position for this row.
                CalculateRowOffsets(row, out xOffset, out yOffset);

                //Get the current row.
                RowInfo currentRow = _rowCache.GetRow(row);

                //Now we can lay out this row.
                for (int page = currentRow.FirstPage; page < currentRow.FirstPage + currentRow.PageCount; page++)
                {
                    //This should never, ever happen so we'll throw if it does.
                    if (visualChild > _childrenCollection.Count - 1)
                    {
                        throw new InvalidOperationException(SR.DocumentGridVisualTreeOutOfSync);
                    }

                    //Get the cached size of this page.
                    Size pageSize = _pageCache.GetPageSize(page);

                    //Scale it by our scale factor
                    pageSize.Width *= Scale;
                    pageSize.Height *= Scale;

                    //Arrange the page if necessary
                    UIElement uiPage = _childrenCollection[visualChild] as UIElement;
                    if (uiPage != null)
                    {
                        Point pageOffset;
                        //Move the page to the right place based on the FlowDirection of the content.
                        if (_pageCache.IsContentRightToLeft)
                        {
                            pageOffset = new Point(Math.Max(ViewportWidth, ExtentWidth) - (xOffset + pageSize.Width), yOffset);

View on GitHub (pinned to 81131a70a4)