Skip to main content
Home Forums Silverlight Programming Programming with .NET - General How can to create a custom event in SilverLight
3 replies. Latest Post by MarkTap on December 19, 2007.
(0)
haughtycool
Member
81 points
35 Posts
12-18-2007 11:24 PM |
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
MarkTap
Participant
1442 points
263 Posts
12-19-2007 12:18 AM |
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; }}
12-19-2007 1:13 AM |
Thanks for you quickly reply.
Is the custom event in SilverLight similar with the custom event in pure .Net framework?
12-19-2007 10:17 AM |
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).