Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

MouseLeftButtonDown Event

Occurs when the left mouse button is depressed.

XAML
<object MouseLeftButtonDown="eventhandlerFunction".../>
Scripting
[token = ]object.AddEventListener("MouseLeftButtonDown", 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 MouseLeftButtonDown event can be defined for any UIElement-derived class, such as Canvas, TextBlock, or Rectangle.

The MouseLeftButtonDown event is fired when the left mouse button is depressed when the mouse is over the object. When the mouse is released, the MouseLeftButtonUp event is fired. However, if the mouse if moved over another object when it is released, the object that received the MouseLeftButtonDown event will not receive the MouseLeftButtonUp event. There is no discrete double click event. A double click is simply two sequences of MouseLeftButtonDown and MouseLeftButtonUp events.

The MouseLeftButtonDown event is a bubbling event. This means that if there multiple MouseLeftButtonDown events defined for a tree of elements, the event is received by each object in the object hierarchy, starting with the object that directly receives the event and then bubbling to each successive parent element. The "bubbling" metaphor indicates that the event starts at the bottom and works its way up the object tree. For a bubbling event, sender in the arguments is the object where the event is handled, not necessarily the object that actually received the input condition that initiated the event.

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

object.AddEventListener("MouseLeftButtonDown", "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.

Examples

The following XAML example that illustrates event bubbling. It shows MouseLeftButtonDown events defined for a two Canvas and one TextBlock objects. In this case, if you depress the mouse over the TextBlock object, it receives the MouseLeftButtonDown. The event is then bubbled upward to the parent Canvas, and finally to the top-level Canvas object.

XAML
<!-- The TextBlock event occurs first. -->
<Canvas 
  xmlns="http://schemas.microsoft.com/client/2007"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  MouseLeftButtonDown="rootCanvasMouseLeftButtonDown">

  <Canvas
    Canvas.Left="20" Canvas.Top="20"
    MouseLeftButtonDown="canvasMouseLeftButtonDown">

    <TextBlock 
      x:Name="myTextBlock" 
      Canvas.Left="25" Canvas.Top="5"
      MouseLeftButtonDown="textMouseLeftButtonDown"
      FontSize="24"
      Text="Click me" />

  </Canvas>
</Canvas>

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

JavaScript
function rootCanvasMouseLeftButtonDown(sender, mouseEventArgs)
{
    // Set the TextBlock to display the mouse position.
    sender.findName("myTextBlock").text = "x = " + mouseEventArgs.getPosition(null).x + "  y = " + mouseEventArgs.getPosition(null).y;
}

Applies To

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

See Also

Silverlight Events
Silverlight Mouse Support
MouseEnter
MouseLeave
MouseLeftButtonUp
MouseMove
GetPosition

Community Additions

Show:
© 2015 Microsoft