dotnet/wpf · error

Specified index is out of range or child at index is null…

Error message

Specified index is out of range or child at index is null. Do not call this method if VisualChildrenCount returns zero, indicating that the Visual has no children.

What it means

DocumentPageView.GetVisualChild(index) only supports index 0 and only when the internal _pageHost exists; any other index (or a disposed/null page host) throws ArgumentOutOfRangeException. DocumentPageView hosts exactly one visual child (the DocumentPageHost).

Solutions

  1. Only request index 0 and verify VisualChildrenCount > 0 first
  2. Avoid calling GetVisualChild on a disposed DocumentPageView; re-check its lifetime
  3. Use VisualTreeHelper.GetChild within the valid count

Example fix

// before
var host = pageView.GetVisualChild(0); // after Dispose -> throws
// after
if (pageView.VisualChildrenCount > 0)
    var host = pageView.GetVisualChild(0);
Defensive patterns

Strategy: validation

Validate before calling

if (!pageView.IsDisposed && pageView.VisualChildrenCount > 0) { var host = pageView.GetVisualChild(0); }

Type guard

static bool HasPageHost(DocumentPageView pv) => !pv.IsDisposed && pv.VisualChildrenCount > 0;

Try / catch

try { var host = pageView.GetVisualChild(0); }
catch (ArgumentOutOfRangeException) { /* view disposed or has no page host */ }

Prevention

When it happens

Trigger: Calling GetVisualChild with an index other than 0, or calling it after Dispose() has cleared _pageHost (when VisualChildrenCount is zero).

Common situations: Visual tree walking over FlowDocumentPageViewer/DocumentPageView content, or touching views after the viewer has been disposed or the document unloaded.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/9d8cacc68e6e3192. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Controls/Primitives/DocumentPageView.cs:472

                    // Size for the page host needs to be set to finalSize

                    // Use previous offset and transform
                    _pageHost.Arrange(new Rect(_pageHost.CachedOffset, finalSize));
                }
            }

            return base.ArrangeOverride(finalSize);
        }

        /// <summary>
        /// Derived class must implement to support Visual children. The method must return
        /// the child at the specified index. Index must be between 0 and GetVisualChildrenCount-1.
        /// </summary>
        protected override Visual GetVisualChild(int index)
        {
            if (index != 0 || _pageHost == null)
            {
                throw new ArgumentOutOfRangeException(nameof(index), index, SR.Visual_ArgumentOutOfRange);
            }
            return _pageHost;
        }

        /// <summary>
        /// Dispose the object.
        /// </summary>
        protected void Dispose()
        {
            if (!_disposed)
            {
                _disposed = true;

                // Cleanup all state associated with Paginator.
                if (_documentPaginator != null)
                {
                    _documentPaginator.GetPageCompleted -= new GetPageCompletedEventHandler(HandleGetPageCompleted);
                    _documentPaginator.PagesChanged -= new PagesChangedEventHandler(HandlePagesChanged);

View on GitHub (pinned to 81131a70a4)