Attached Events in Silverlight == false

As I am digging more and more into Silverlight I find more and more features I have come to love in WPF that are just not there. One feature is Attached Events. In WPF I can do something like this in XAML.

<StackPanel Height="25"  Orientation="Horizontal"  ButtonBase.Click="StackPanelCommon_Click">
    <Button Name="Yes" Margin="5,0,5,0" Content="Yes"/>
    <Button Name="No" Margin="5,0,5,0" Content="No"/>
    <Button Name="Cancel" Margin="5,0,5,0" Content="Cancel"/>
</StackPanel>

In this scenario the StackPanelCommon_Click handler will handle the click event for the Yes, No and Cancel buttons. Inside this handler I can add code to determine the particular button selected and implement logic accordingly (see below).

private void StackPanelCommon_Click(object sender, RoutedEventArgs e)
{
  Button button = e.Source as Button;
  if(button != null)
  {
    switch (button.Name)
    {
      case "Yes":
        //some logic goes here
        break;
      case "No":
        //some logic goes here
        break;
      case "Cancel":
        //some logic goes here
        break;
    }
  }
}

But when I try this in Silverlight I get the following error.

So what the heck is going on here? It turns out that UIElement does not support the AddHandler method which allows you to add handlers to the UIElement.

I hope in future versions Microsoft can add this into Silverlight to help promote more concise coding approaches since the alternative is similar to the old days gone by.

 

Leave a Reply

Your email address will not be published. Required fields are marked *