Friday, October 14, 2011

WPF - C# - Custom Event

One of the things I sometimes need is to create a custom event for sending an object or simply annunciate that an action has occurred.

I want an event to be dispatched on a control - child - and be collected on another control - parent.

1. Create the event on the child side
public delegate void FinishedReadingEventHandler(string str);
public event FinishedReadingEventHandler FinishedReadingEvent;

2. When I want to dispatch the event I do this:
3. On the parent side I need to have a listener waiting for that event dispatch:

ChildCtrl.CitizenTypeEvent += new ChildControl.CitizenTypeEventHandler(ChildCtrl_CitizenTypeEvent);

4. And the method for executing wherever actions there are with equal arguments as my delegate

Here is a good reference for another example:

No comments: