dotnet/wpf · error

SR.DocumentGridVisualTreeContainsNonDocumentGridPage

Error message

SR.DocumentGridVisualTreeContainsNonDocumentGridPage

What it means

InvalidOperationException from DocumentGrid.MeasureOverride: a child in the visual collection after the background is not a DocumentGridPage, meaning someone modified the internal visual tree. The throw guards the assumption that all measured pages are DocumentGrid pages.

Solutions

  1. Avoid manipulating the DocumentGrid's visual children; use the documented paging APIs
  2. If subclassing, restore the expected child types before the layout pass
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs:1062 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/8b0f107481d2e9cf. Report an issue: GitHub.

Appendix: source

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

                    if (background == null)
                    {
                        throw new InvalidOperationException(SR.DocumentGridVisualTreeContainsNonBorderAsFirstElement);
                    }

                    //We measure this to the size of our constraint.
                    background.Measure(constraint);
                }
                //Otherwise it's a page.
                else
                {
                    //Ensure that this is actually a DocumentGridPage.  If it is not,
                    //Then someone's been mucking with our VisualTree, so we'll throw.
                    DocumentGridPage page = _childrenCollection[i] as DocumentGridPage;

                    if (page == null)
                    {
                        throw new InvalidOperationException(SR.DocumentGridVisualTreeContainsNonDocumentGridPage);
                    }

                    //Get the cached size of this page and scale it to our current scale factor.
                    Size pageSize = _pageCache.GetPageSize(page.PageNumber);
                    pageSize.Width *= Scale;
                    pageSize.Height *= Scale;

                    //Measure the page if necessary.
                    if (!page.IsMeasureValid)
                    {
                        page.Measure(pageSize);

                        //See if the cached size has changed since we Measured.
                        //This can happen if in the course of Measuring the page
                        //a GetPageAsync() calls back immediately with the real page size.
                        //If this happens we need to re-measure the page before we finish here,
                        //otherwise we'll end up with a page that's Measured to one size
                        //and Arranged to another, which looks bad.

View on GitHub (pinned to 81131a70a4)