MouseEventArgs.GetPosition Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

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

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

Syntax

'Declaration
Public Function GetPosition ( _
    relativeTo As UIElement _
) As Point
public Point GetPosition(
    UIElement relativeTo
)

Parameters

  • relativeTo
    Type: System.Windows.UIElement
    Any UIElement-derived object that is contained by the Silverlight plug-in and connected to the object tree. To specify the object relative to the overall Silverlight coordinate system, use a relativeTo value of nulla null reference (Nothing in Visual Basic).

Return Value

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

Remarks

The most common usage is to pass relativeTo as nulla null reference (Nothing in Visual Basic), which returns a coordinate position of the mouse within the overall Silverlight plug-in content area.

The relativeTo parameter can also be set to any UIElement-derived object that is contained by the Silverlight plug-in. The returned mouse 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 mouse 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 nulla null reference (Nothing in Visual Basic) 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 mouse 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 a mouse 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.

If the Windowless property is true for the Silverlight plug-in, Silverlight can only provide information about the mouse while the mouse pointer is over the Silverlight plug-in's content area . If Windowless is true, a captured mouse will continue to update its position even when the pointer is no longer over the area that raised the mouse event, but will cease updating position (and lose capture) if the pointer exits the Silverlight plug-in content area entirely (moves to a different area of the hosting HTML page, or off the browser window entirely and onto the desktop). If Windowless is false, a captured mouse will continue to update its position when the pointer exits the Silverlight plug-in content area or the browser window that hosts the plug-in.

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 Silverlight 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 Silverlight content area from the HTML DOM, and add that offset to the X and Y values from the Silverlight mouse position to get a browser-relative coordinate. This might be useful if you are working with HTML overlays.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.