does not have as much support to handle events from unreferenced child
controls as WPF. For example, say you have the following control
Page needs to handle events from the Child Controls.
is easy in WPF since you can use commands,
you can declare the event handler in XAML.
// subscribe to events
raised by any child, Child Control is a Button in this example
this.AddHandler(Button.ClickEvent, new RoutedEventHandler(ChildClicked));
private void ChildClicked(object sender, RoutedEventArgs e)
// called when event is raised by any child
to Silverlight, it
does not support commands or UIElement.AddHandler. There are several
Title="WPF Events" Height="200" Width="200"
Hookup each event handler
You can specify the event handler for each child control but that is
the whole point... we want to handle events
even though we don't have a reference to the individual child controls.
hooking up events requires a reference to each control
this.ChildControl1.Click += new RoutedEventHandler(ChildControl1_Click);
this.ChildControl2.Click += new RoutedEventHandler(ChildControl2_Click);
this.ChildControl3.Click += new RoutedEventHandler(ChildControl3_Click);
this.ChildControl4.Click += new RoutedEventHandler(ChildControl4_Click);
You can write code that bubbles up the events (re-raises the events).
This usually becomes undesirable, especially when you have controls
nested at several levels.
Use global Events class
One option is to create a global Events class.
is only one instance of the Events class in the application.
contains a list of global events and helper methods to raise the events.
object can subscribe to events.
object can raise events.
means the main page can handle events from any child control, children
can handle events from the main page, and siblings can handle events
from each other.
reference to the control is not required to handle events.
public static class Events
of raising an event.
// example global event that can be handled
by any object in the application
public static event EventHandler ChildControlClicked;
public static void RaiseChildControlClicked(object sender, EventArgs args)
if (ChildControlClicked != null)
// raise global event
of handling an event.
// subscribe to global event
Events.ChildControlClicked += new System.EventHandler(Events_ChildControlClicked);
private void Events_ChildControlClicked(object sender, System.EventArgs e)
// called when the event is raised, it could be raised by
// any object in the
application, this object does not need
// a reference to the object that raised