Suppose I wanted to do this, so I can find the current position of the mouse relative to a Visual, without needing access to a specific mouse event:
public static Point GetMousePosition(this Visual relativeTo)
{
return relativeTo.PointFromScreen(GetMousePositionOnScreen());
}
Sometimes (usually when I've just switched between two tab controls) PointFromScreen throws an InvalidOperationException with the message This Visual is not connected to a PresentationSource.
On looking at the properties available on Visual I can't see any relating to a PresentationSource.
Given a Visual, how can I tell if it is going to throw that exception when I call PointFromScreen on it?