{"record":{"id":"f1ed3f812a9e0c83","repo":"dotnet/wpf","slug":"only-one-documentpageview-with-ismasterpage-property-set-to","errorCode":null,"errorMessage":"Only one DocumentPageView with IsMasterPage property set to True allowed.","messagePattern":"Only one DocumentPageView with IsMasterPage property set to True allowed\\.","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs","lineNumber":874,"sourceCode":"        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\n        /// <summary>\n        /// Does deep Visual tree walk to retrieve all DocumentPageViews.\n        /// It stops recursing down into visual tree in following situations:\n        /// a) Visual is UIElement and it is not part of Contol Template,\n        /// b) Visual is DocumentPageView.\n        /// </summary>\n        /// <param name=\"root\">FrameworkElement that is part of Control Template.</param>\n        /// <param name=\"pageViews\">Collection of DocumentPageViews; found elements are appended here.</param>\n        /// <returns>Whether collection of DocumentPageViews has been updated.</returns>\n        private void FindDocumentPageViews(Visual root, List<DocumentPageView> pageViews)","sourceCodeStart":856,"sourceCodeEnd":892,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs#L856-L892","documentation":"Among the DocumentPageView objects in a DocumentViewerBase's PageViews collection, exactly one at most may have the attached property IsMasterPage set to true; it designates the master page used for zooming and layout. VerifyDocumentPageViews throws ArgumentException if two or more views declare IsMasterPage=true.","triggerScenarios":"Setting IsMasterPage=true on two or more DocumentPageView instances in the same PageViews collection (in XAML or code) before assigning it to the viewer.","commonSituations":"Copying a DocumentPageView element in XAML (including its IsMasterPage attribute) to create multiple pages, forgetting to reset IsMasterPage on the copies.","solutions":["Ensure only one DocumentPageView has IsMasterPage=\"True\"","Remove the IsMasterPage attribute from duplicated views or set it to False","Set IsMasterPage programmatically after building the collection, guarding so only the first view gets it"],"exampleFix":"// before\n<vw:DocumentPageView IsMasterPage=\"True\"/>\n<vw:DocumentPageView IsMasterPage=\"True\"/>\n// after\n<vw:DocumentPageView IsMasterPage=\"True\"/>\n<vw:DocumentPageView/>","handlingStrategy":"validation","validationCode":"int masters = pageViews.Cast<DocumentPageView>().Count(pv => DocumentViewerBase.GetIsMasterPage(pv));\nif (masters > 1) { /* unset IsMasterPage on all but the first */ }","typeGuard":null,"tryCatchPattern":"try { viewer.PageViews = pageViews; }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"IsMasterPage\")) { /* fix master page flags */ }","preventionTips":["Mark exactly one view as master (or none)","When duplicating DocumentPageView elements in XAML, strip IsMasterPage from copies","Compute the master flag programmatically after composing the list"],"tags":["wpf","documentviewer","validation"],"backgroundTag":"invalid-argument-value","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"}