{"record":{"id":"1b666da490910e6e","repo":"dotnet/wpf","slug":"sr-documentgridvisualtreecontainsnonborderasfirstelement","errorCode":null,"errorMessage":"SR.DocumentGridVisualTreeContainsNonBorderAsFirstElement","messagePattern":"SR\\.DocumentGridVisualTreeContainsNonBorderAsFirstElement","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs","lineNumber":1047,"sourceCode":"            }\n\n            //Determine which pages are visible at the current offset given the current constraint.\n            RecalculateVisualPages(VerticalOffset, constraint);\n\n            //Get our visual children count...\n            int count = _childrenCollection.Count;\n\n            //Now go through our child collection and measure all the pages to their sizes.\n            for (int i = 0; i < count; i++)\n            {\n                //This should be our background.\n                if (i == _backgroundVisualIndex)\n                {\n                    Border background = _childrenCollection[i] as Border;\n\n                    if (background == null)\n                    {\n                        throw new InvalidOperationException(SR.DocumentGridVisualTreeContainsNonBorderAsFirstElement);\n                    }\n\n                    //We measure this to the size of our constraint.\n                    background.Measure(constraint);\n                }\n                //Otherwise it's a page.\n                else\n                {\n                    //Ensure that this is actually a DocumentGridPage.  If it is not,\n                    //Then someone's been mucking with our VisualTree, so we'll throw.\n                    DocumentGridPage page = _childrenCollection[i] as DocumentGridPage;\n\n                    if (page == null)\n                    {\n                        throw new InvalidOperationException(SR.DocumentGridVisualTreeContainsNonDocumentGridPage);\n                    }\n\n                    //Get the cached size of this page and scale it to our current scale factor.","sourceCodeStart":1029,"sourceCodeEnd":1065,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs#L1029-L1065","documentation":"InvalidOperationException from DocumentGrid.MeasureOverride: the first visual child (the background) is not a Border, which means the control's internal visual tree was modified by external code. The throw is an integrity check that the tree DocumentGrid built is still intact.","triggerScenarios":"Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/MS/Internal/documents/DocumentGrid.cs:1047 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Do not add or remove children of the DocumentGrid visual collection; treat its visual tree as internal","Derive from DocumentViewerBase-level APIs instead of manipulating the DocumentGrid's children"],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}