Some technical things, mostly odd happenings from a remote office RSS Feed


Silverlight, handling events for nested controls
Silverlight does not have as much support to handle events from unreferenced child controls as WPF. For example, say you have the following control hierarchy and Page needs to handle events from the Child Controls.

This is easy in WPF since you can use commands, or UIElement.AddHandler. For example:
public MainWindow()
{
    InitializeComponent();

    // 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
}
Or you can declare the event handler in XAML.
<Window x:Class="WpfEvents.MainWindow"
  xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
  xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  xmlns:controls="clr-namespace:WpfEvents"
  Title="WPF Events" Height="200" Width="200"
  Button.Click="ChildClicked">
Back to Silverlight, it does not support commands or UIElement.AddHandler. There are several options:

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);
Bubble up the events
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.

  • There is only one instance of the Events class in the application.
  • It contains a list of global events and helper methods to raise the events.
  • Any object can subscribe to events.
  • Any object can raise events.
  • That 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.
  • A reference to the control is not required to handle events.
Example Events class.
public static class Events
{
    // 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)
        {
            ChildControlClicked(sender,args);
        }
    }
}
Example of raising an event.
// raise global event
Events.RaiseChildControlClicked(thisEventArgs.Empty);
Example of handling an event.
public Page()
{
    InitializeComponent();
    
    // 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 the event
}

 
Posted by Ralph Arvesen | 1 Comments | Trackback Url | Bookmark with:        
Tags:

Links to this Post

Comments

Monday, 26 Jan 2009 05:38 by Tom Duijkers
Thank you very much! This works very well and saved me a lot of time.

Name:
URL:
Email:
Comments:

CAPTCHA Image Validation