dotnet/wpf · error
Cannot have empty collection of DocumentPageView objects.
Error message
Cannot have empty collection of DocumentPageView objects.
What it means
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.
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
Example fix
// before
documentViewer.PageViews = new DocumentPageViewList();
// after
documentViewer.PageViews = new DocumentPageViewList { new DocumentPageView() }; Defensive patterns
Strategy: validation
Validate before calling
if (pageViews == null || pageViews.Count == 0) throw new InvalidOperationException("At least one DocumentPageView is required before assigning PageViews."); Try / catch
try { viewer.PageViews = pageViews; }
catch (ArgumentException ex) when (ex.Message.Contains("DocumentPageView")) { /* supply a non-empty collection */ } Prevention
- 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
When it happens
Trigger: Assigning a null or empty DocumentPageViewList to DocumentViewerBase.PageViews (e.g. via XAML <DocumentViewer.PageViews/> or code), which triggers UpdatePageViews -> VerifyDocumentPageViews.
Common situations: Declaratively defining a DocumentViewer with no PageViews in XAML, clearing the PageViews collection at runtime, or data-binding an empty collection into PageViews.
Understand the failure class
Background: "must not be empty", "cannot be empty" — required-field validation errors across open-source libraries — this error's family across 41 libraries.
Related errors
- Only one DocumentPageView with IsMasterPage property set to…
- Bracket characters cannot be alpha-numeric or whitespace.
- Bracket characters cannot be one of the following: '=' …
- DocumentViewer element can have only one child.
- InvalidDrawingAttributesWidth
AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14).
Data as JSON: /api/errors/ea9818f8087dca29.
Report an issue: GitHub.
Appendix: source
Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentViewerBase.cs:864
}
}
/// <summary>
/// Verify collection of DocumentPageViews. It needs to meet following conditions:
/// a) collection is not null,
/// b) only one page has IsMasterPage property set to true,
/// c) unique PageNumbers for each active DocumentPageView,
/// </summary>
/// <param name="pageViews">Collection of DocumentPageViews to validate.</param>
private void VerifyDocumentPageViews(ReadOnlyCollection<DocumentPageView> pageViews)
{
int index;
bool hasMasterPage = false;
// At least one DocumentPageView is required.
if (pageViews == null)
{
throw new ArgumentException(SR.DocumentViewerPageViewsCollectionEmpty);
}
// Expecting only one DocumentPageView with IsMasterPage property set to true.
for (index = 0; index < pageViews.Count; index++)
{
if (GetIsMasterPage(pageViews[index]))
{
if (hasMasterPage)
{
throw new ArgumentException(SR.DocumentViewerOneMasterPage);
}
hasMasterPage = true;
}
}
// Unique PageNumbers for each active DocumentPageView.
}
View on GitHub (pinned to 81131a70a4)