LostFocus Event

Occurs when an object loses focus.

XAML
<object LostFocus="eventhandlerFunction" .../>
Scripting
[token = ]object.AddEventListener("LostFocus", 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.

eventArgs

object

This parameter is always set to null.

Remarks

In Silverlight 1.0, the GotFocus event can only be defined for the root object, which is typically a Canvas. It is technically possible to add LostFocus event handlers for non-Canvas objects, but only if that object is the root element. Non-Canvas roots are uncommon and typically not very useful in Silverlight 1.0. Therefore, other UIElement objects are not listed in the Applies To section of this reference page, and conversely UIElement types other than Canvas do not list LostFocus as an event.

Attempting to add a handler for LostFocus to an object that is not the root will raise either an initializion error in XAML or a run time error if you attach the handler in code.

Use the LostFocus event to specify actions when the Silverlight content loses focus in the browser DOM.

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

object.AddEventListener("LostFocus", "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 shows GotFocus and LostFocus events defined for the root Canvas object:

XAML
<Canvas
  xmlns="https://schemas.microsoft.com/client/2007"
  xmlns:x="https://schemas.microsoft.com/winfx/2006/xaml"
  GotFocus="onGotFocus"
  LostFocus="onLostFocus">
  <TextBlock x:Name="myTextBlock" />
</Canvas>

The following JavaScript example shows how to implement GotFocus and LostFocus event handler functions. In this case, the text in the TextBlock changes opacity when focus is received or lost:

JavaScript
function onGotFocus(sender, eventArgs)
{
    sender.findName("myTextBlock").Text = "got focus";
}
function onLostFocus(sender, eventArgs)
{
    sender.findName("myTextBlock").Text = "lost focus";
}

Applies To

Canvas

See Also

Silverlight Events
Silverlight Keyboard Support
GotFocus