Freigeben über


MouseLeave

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

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

<object MouseLeave="eventhandlerFunction" .../>
[token = ]object.AddEventListener("MouseLeave", eventhandlerFunction)

Arguments

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, quotation marks around the function name are not required. (See the "Remarks" section.)

Event Handler Parameters

sender

object

The object that invoked the event.

mouseEventArgs

object

This parameter is always set to null.

Managed Equivalent

MouseLeave

Remarks

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

The MouseLeave event is raised in response to the mouse leaving the object's bounding area. It will be raised in conjunction with MouseMove.

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

object.AddEventListener("MouseLeave", "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 MouseLeave events for objects in XAML content. However, if a child object and its parent object both define a MouseLeave event, the parent object's MouseLeave event occurs before the child object's MouseLeave event. This is not a case of a bubbling event; it indicates only that the mouse has entered both objects, potentially at different times depending on the object layout.

Unlike the MouseEnter event, the MouseLeave event does not provide the mouse position as an event argument, because the location of the mouse position relative to the object is unknown when it is out of bounds. Because it is always null, you generally should omit the mouseEventArgs parameter for the MouseLeave event handler function, although you may still be interested in the sender parameter.

Example

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

<!-- The Canvas MouseLeave event fires first, then the TextBlock MouseLeave event -->
<Canvas
  xmlns="https://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  MouseLeave="rootCanvasMouseLeave">

  <TextBlock
   x:Name="captionTextBlock" 
   MouseLeave="textBlockMouseLeave"
   Text="Test order of MouseLeave events" />

</Canvas>

The following JavaScript example shows how to implement a MouseLeave event handler function.

function rootCanvasMouseLeave(sender)
{   
    // Remove the visibility of the caption TextBlock.
    sender.findName("captionTextBlock").opacity = 0;
}

Applies To

Border (Silverlight 2)

Canvas

Ellipse

Glyphs

Image

InkPresenter

Line

MediaElement

PasswordBox (Silverlight 2)

Path

Polygon

Polyline

Popup (Silverlight 2)

Rectangle

StackPanel (Silverlight 2)

TextBlock

TextBox (Silverlight 2)