MouseEnter Event

Occurs when the mouse enters the bounding area of an object.

XAML
<object MouseEnter="eventhandlerFunction" .../>
Scripting
[token = ]object.AddEventListener("MouseEnter", eventhandlerFunction)

AddEventListener Parameters

token

integer

A token that is returned from the function, which you can optionally retain as a variable. If you intend to call RemoveEventListener to remove the handler, you will need this token.

eventhandlerFunction

object

The name of your event handler function as it is defined in script. When used as an AddEventListener parameter, quotes around the function name are not required. See Remarks.

Event Handler Parameters

sender

object

Identifies the object that invoked the event.

mouseEventArgs

object

mouseEventArgs.GetPosition(element).X identifies the x-coordinate position of the mouse.

mouseEventArgs.GetPosition(element).Y identifies the y-coordinate position of the mouse.

mouseEventArgs.Shift determines whether the SHIFT key is down.

mouseEventArgs.Ctrl determines whether the CTRL key is down.

Remarks

The MouseEnter event can be defined for any UIElement-derived class, such as Canvas, TextBlock, or Rectangle.

The MouseEnter event is typically fired in response to the mouse into the object's bounding area. In this case, the MouseEnter event precedes the MouseMove event for the object. However, it is possible to receive a MouseEnter event without a MouseMove event, in the following cases:

  • The object, rather than the mouse, is moved under the cursor position.
  • The object is capturing mouse events. When an object captures the mouse, it receives mouse input whether or not the cursor is within its borders. The mouse is typically captured in this manner only during drag-and-drop operations, and retains capture until the drop action of the drag-and-drop operation occurs.

You can also add handlers in script using a quoted string for the event handler name:

object.AddEventListener("MouseEnter", "eventhandlerFunction")

This syntax also returns a token; however, the token is not an absolute requirement for removing the handler, in cases where the handler was added by using a quoted string. For details, see RemoveEventListener.

You can define multiple MouseEnter events for objects in XAML content. However, if a child object and its parent object both define a MouseEnter event, the parent object's MouseEnter event is fired before the child object's MouseEnter event. This is not a case of a bubbling event, it only indicates that the mouse has entered both objects, potentially at different times depending on the object layout.

The mouse position reported in the mouseEventArgs parameter value may not be exactly on the boundary of the object, due to the coalescing of mouse movements.

Examples

The following XAML example shows MouseEnter events defined for a Canvas and TextBlock object. In this case, the Canvas object's event handler function is invoked before the TextBlock object's event handler function.

XAML
<!-- Canvas MouseEnter event fires first, then TextBlock MouseEnter event -->
<Canvas
  xmlns="https://schemas.microsoft.com/client/2007"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  MouseEnter="rootCanvasMouseEnter">
  
  <TextBlock
    x:Name="myTextBlock" 
    MouseEnter="textBlockMouseEnter"
    Text="Test order of MouseEnter events" />
</Canvas>

The following JavaScript example shows how to implement a MouseEnter event handler function:

JavaScript
function rootCanvasMouseEnter(sender, mouseEventArgs)
{   
    // Concatenate the values of the MouseEventArgs parameter.
    var msg  = "x = " + mouseEventArgs.getPosition(null).x;
        msg += "  y = " + mouseEventArgs.getPosition(null).y;
        msg += "  shift = " + mouseEventArgs.shift;
        msg += "  ctrl = " + mouseEventArgs.ctrl;
    // Display the values of the MouseEventArgs parameter.
    sender.findName("myTextBlock").text = msg;
}

Applies To

Canvas, Ellipse, Glyphs, Image, InkPresenter, Line, MediaElement, Path, Polygon, Polyline, Rectangle, TextBlock

See Also

Silverlight Events
Silverlight Mouse Support
MouseLeave
MouseLeftButtonDown
MouseLeftButtonUp
MouseMove
GetPosition