GetPosition Method
Collapse the table of content
Expand the table of content

MouseEventArgs.GetPosition Method

[ This article is for Windows Phone 8 developers. If you’re developing for Windows 10, see the latest documentation. ]

Returns the x- and y-coordinates of the pointer position, optionally evaluated against a coordinate origin of a supplied UIElement.

Namespace:  System.Windows.Input
Assembly:  System.Windows (in System.Windows.dll)

public Point GetPosition(
	UIElement relativeTo


Type: System.Windows.UIElement
Any UIElement-derived object that is contained by the Windows Phone and connected to the object tree. To specify the object relative to the overall Windows Phone coordinate system, use a relativeTo value of null.

Return Value

Type: System.Windows.Point
A Point that represents the current x- and y-coordinates of the pointer position. If null was passed as relativeTo, this coordinate is for the overall Windows Phone content area. If a non-null relativeTo was passed, this coordinate is relative to the object referenced by relativeTo.

The most common usage is to pass relativeTo as null, which returns a coordinate position of the pointer within the overall Windows Phone content area.

The relativeTo parameter can also be set to any UIElement-derived object that is contained by the Windows Phone. The returned pointer position is relative to the object referenced as relativeTo. Depending on the location of relativeTo relative to the object, the coordinate values of the pointer position may be negative values.

If you are handling the event from the root element of the visual tree and FlowDirection equals LeftToRight, passing the root element as relativeTo or passing null are equivalent.

The UIElement that is passed as relativeTo must be connected to the current object hierarchy, in such a way that successive resolution of parents is connected to the root element. Passing a UIElement that is disconnected will generate an error.

Because some pointer events bubble, the sender parameter of the event handler that also received the event data may or may not be suitable for use as a frame of reference for the relativeTo parameter of GetPosition. Make sure that your handler for an event that attempts to use sender for a GetPosition call is being handled by the expected object, and has not bubbled upwards, perhaps being handled by the root element if you also have a handler there.

The position returned by GetPosition is not the position of the client user's overall screen, or even of the hosting browser, it is purely a coordinate space used by Windows Phone in its content area. Depending on what information a given browser exposes to its DOM, it is often possible to obtain a browser positional offset for the Windows Phone content area from the HTML DOM, and add that offset to the X and Y values from the Windows Phone  pointer position to get a browser-relative coordinate. This might be useful if you are working with HTML overlays.

Windows Phone OS

Supported in: 8.1, 8.0, 7.1, 7.0

Windows Phone

© 2016 Microsoft