{"record":{"id":"ea9818f8087dca29","repo":"dotnet/wpf","slug":"cannot-have-empty-collection-of-documentpageview-objects","errorCode":null,"errorMessage":"Cannot have empty collection of DocumentPageView objects.","messagePattern":"Cannot have empty collection of DocumentPageView objects\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs","lineNumber":864,"sourceCode":"            }\n        }\n\n        /// <summary>\n        /// Verify collection of DocumentPageViews. It needs to meet following conditions:\n        /// a) collection is not null,\n        /// b) only one page has IsMasterPage property set to true,\n        /// c) unique PageNumbers for each active DocumentPageView,\n        /// </summary>\n        /// <param name=\"pageViews\">Collection of DocumentPageViews to validate.</param>\n        private void VerifyDocumentPageViews(ReadOnlyCollection<DocumentPageView> pageViews)\n        {\n            int index;\n            bool hasMasterPage = false;\n\n            // At least one DocumentPageView is required.\n            if (pageViews == null)\n            {\n                throw new ArgumentException(SR.DocumentViewerPageViewsCollectionEmpty);\n            }\n\n            // Expecting only one DocumentPageView with IsMasterPage property set to true.\n            for (index = 0; index < pageViews.Count; index++)\n            {\n                if (GetIsMasterPage(pageViews[index]))\n                {\n                    if (hasMasterPage)\n                    {\n                        throw new ArgumentException(SR.DocumentViewerOneMasterPage);\n                    }\n                    hasMasterPage = true;\n                }\n            }\n\n            // Unique PageNumbers for each active DocumentPageView.\n        }\n","sourceCodeStart":846,"sourceCodeEnd":882,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs#L846-L882","documentation":"DocumentViewerBase.VerifyDocumentPageViews requires a non-empty collection of DocumentPageView objects to build the viewer's page views. A null or empty PageViews collection is invalid because the viewer cannot render any pages without at least one view.","triggerScenarios":"Assigning a null or empty DocumentPageViewList to DocumentViewerBase.PageViews (e.g. via XAML <DocumentViewer.PageViews/> or code), which triggers UpdatePageViews -> VerifyDocumentPageViews.","commonSituations":"Declaratively defining a DocumentViewer with no PageViews in XAML, clearing the PageViews collection at runtime, or data-binding an empty collection into PageViews.","solutions":["Add at least one DocumentPageView to the PageViews collection","If PageViews is data-bound, ensure the source collection is populated before assignment","If the viewer should be empty, avoid setting PageViews at all rather than assigning an empty collection"],"exampleFix":"// before\ndocumentViewer.PageViews = new DocumentPageViewList();\n// after\ndocumentViewer.PageViews = new DocumentPageViewList { new DocumentPageView() };","handlingStrategy":"validation","validationCode":"if (pageViews == null || pageViews.Count == 0) throw new InvalidOperationException(\"At least one DocumentPageView is required before assigning PageViews.\");","typeGuard":null,"tryCatchPattern":"try { viewer.PageViews = pageViews; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"DocumentPageView\")) { /* supply a non-empty collection */ }","preventionTips":["Validate PageViews is non-empty before assignment","In XAML, always include at least one DocumentPageView inside PageViews","Guard data-bound collections so they are populated before binding"],"tags":["wpf","documentviewer","validation"],"backgroundTag":"empty-required-field","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}