dotnet/wpf · error · InvalidOperationException

SR.Visual_NoPresentationSource

Error message

SR.Visual_NoPresentationSource

What it means

Visual.PointToScreen throws InvalidOperationException(SR.Visual_NoPresentationSource) when PresentationSource.FromVisual(this) returns null, meaning the visual is not yet connected to a presentation source (not rendered in a window/HwndSource). Point-to-screen conversion requires the visual to be live in a presented tree.

Solutions

  1. Call PointToScreen only after the element is loaded (subscribe to Loaded event)
  2. Guard with PresentationSource.FromVisual(visual) != null before calling
  3. Ensure the hosting Window is shown (IsLoaded / IsVisible) before converting coordinates

Example fix

// before
ctor: var screenPt = element.PointToScreen(new Point(0, 0));
// after
element.Loaded += (s, e) =>
{
    if (PresentationSource.FromVisual(element) != null)
        var screenPt = element.PointToScreen(new Point(0, 0));
};
Defensive patterns

Strategy: validation

Validate before calling

if (PresentationSource.FromVisual(visual) == null) return; // not yet presented

Type guard

bool IsPresented(Visual v) => PresentationSource.FromVisual(v) != null;

Try / catch

try { var p = visual.PointToScreen(point); } catch (InvalidOperationException) { /* visual not connected to a presentation source */ }

Prevention

When it happens

Trigger: Calling PointToScreen on a visual before it is loaded/shown, on an element in a non-presented tree, or on an element whose window was closed.

Common situations: Calling in a constructor instead of Loaded; detached elements created but never added to a window; popups/tooltips queried before opening.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/System/Windows/Media/Visual.cs:4723

                    dpi = new DpiScale(UIElement.DpiScaleXValues[actualIndex], UIElement.DpiScaleYValues[actualIndex]);
                }
            }
            return dpi;
        }

        /// <summary>
        /// This method converts a point in the current Visual's coordinate
        /// system into a point in screen coordinates.
        /// </summary>
        public Point PointToScreen(Point point)
        {
            VerifyAPIReadOnly();

            PresentationSource inputSource = PresentationSource.FromVisual(this);

            if (inputSource == null)
            {
                throw new InvalidOperationException(SR.Visual_NoPresentationSource);
            }

            // Translate the point from the visual to the root.
            GeneralTransform gUp = this.TransformToAncestor(inputSource.RootVisual);
            if (gUp == null || !gUp.TryTransform(point, out point))
            {
                throw new InvalidOperationException(SR.Visual_CannotTransformPoint);
            }

            // Translate the point from the root to the screen
            point = PointUtil.RootToClient(point, inputSource);
            point = PointUtil.ClientToScreen(point, inputSource);

            return point;
        }

        /// <summary>
        /// This method converts a point in screen coordinates into a point

View on GitHub (pinned to 81131a70a4)