GotFocus Event
Occurs when an object receives focus.
XAML |
<object GotFocus="eventhandlerFunction" .../>
|
Scripting |
[token = ]object.AddEventListener("GotFocus", 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 GotFocus 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 GotFocus as an event.
Attempting to add a handler for GotFocus to an object (including Canvas) 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 GotFocus event to specify actions when the Silverlight content receives focus in the browser DOM.
You can also add handlers in script using a quoted string for the event handler name:
object.AddEventListener("GotFocus", "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.
In some browsers, the GotFocus event is fired during the initial Silverlight plug-in loading into the HTML page, because of how the particular browser treats focus setting upon initiation in its control or plug-in model.
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 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"; } |