Working with events in Silverlight is quite similar to working with events in ASP.NET, Because the UI for a typical Silverlight-based application is defined in markup (XAML), some of the principles of connecting UI events from markup elements to a runtime code entity are similar to other Web technologies.
We could use JavaScript API for Silverlight to perform event handling for a XAML page.
Adding Silverlight Event Handlers to controls
We could add Silverlight event handlers to controls withing XAML or within the code behind.
For example, we will assign an event handler for a button click event within the XAML like below:
<Button Name="btnClickMe" Content="Click Me" Click="btnClickMe_Click"></Button>
The following code shows how to assign the same event handler to the button click event within the code behind:
btnClickMe.Click += new RoutedEventHandler(btnClickMe_Click);
Removing Silverlight Event Handlers
In some circumstances, we might want to remove event handlers. To remove event handlers, we use the –= operator like below:
btnClickMe.Click -= btnClickMe_Click;
Silverlight Event Handlers Parameters
Any handler we write for a managed Silverlight event has two parameters. The first such parameter is sender, which is a reference to the object where the handler is attached.The sender parameter is typed as the base Object type.Based on our own application design, we expect a type that is safe to cast sender to, and then we could change the sender state.
All events send some kind of event data like the position of the mouse when the mouse events is fired, these data is captured by the second parameter which is an instance of a class that inherits EventArgs (or is EventArgs itself).
Silverlight Routed Events
The routed events are "bubbling" from XAML object element children that raise the event, toward the parent object element that contains them. The event occurrence and its event data continue and are reported to objects along the event route until the root element is reached.
The events are first handled by their source and then the event handlers of the parent elements are invoked till the event reaches the root element of the visual tree.
Silverlight RoutedEventArgs OriginalSource Propert
The OriginalSource property holds the original object that raised the event, instead of where the handler is attached.Take attention that the sender property holds a reference to the object where the handler is attached.
Silverlight RoutedEventArgs Handled Propert
If you want to stop event propagation further through the visual tree, set this property to true.