Skip to main content

Microsoft Silverlight

Answered Question How can to create a custom event in SilverLightRSS Feed

(0)

haughtycool
haughtycool

Member

Member

81 points

35 Posts

How can to create a custom event in SilverLight

Hi all! 

 

How can to create a custom event in SilverLight?

For example: I have my custom list box control and another control. When the user choose an item, this list box fire an event to other controls. Other controls can handle this event

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

MarkTap
MarkTap

Participant

Participant

1442 points

263 Posts

Answered Question

Re: How can to create a custom event in SilverLight

For a simple notification you could do something like this:

public class ListBox : Control
{
     public event EventHandler SelectionChanged;

     ...

        void OnMouseLeftButtonUp(object sender, MouseEventArgs args)
        {
            ...
            if (selectedItem != newSelection)
            {
                ...
                if (SelectionChanged != null)
                {
                    SelectionChanged(this, null);
                }
            }
         }
}
 

If you wanted to get fancier you could define your own delegate type that passed the identity of the selected item in the callback event args:

delegate void SelectionChangedEventHandler(object sender, SelectionChangedEventArgs e);

class SelectionChangedEventArgs : EventArgs
{
   public int SelectedIndex { get; set; }

haughtycool
haughtycool

Member

Member

81 points

35 Posts

Answered Question

Re: Re: How can to create a custom event in SilverLight

 Thanks for you quickly reply.

Is the custom event in SilverLight similar with the custom event in pure .Net framework?

Imagine there's no countries
It isn't hard to do
Nothing to kill or die for
And no religion too
Imagine all the people
Living life in peace

MarkTap
MarkTap

Participant

Participant

1442 points

263 Posts

Re: Re: How can to create a custom event in SilverLight

Yes - if you are in the C# code-behind then it's just like regular .NET.

If you need to interact with HTML elements (e.g. receive a button click event from an HTML button on your form), then it is not exactly the same but is still pretty easy. Jesse Liberty did a great video about how to do this (http://silverlight.net/Learn/learnvideo.aspx?video=572).
 

  • Unanswered Question
  • Answered Question
  • Announcement
Microsoft Communities