Custom events are often used in custom controls. In WPF, you can define events in two ways,
Public partial class usercontrol1: usercontrol {public static readonly routedevent okbuttonclickevent = eventmanager. registerroutedevent ("okbuttonclick", routingstrategy. bubble, typeof (routedeventhandler), typeof (usercontrol1); public event upload okbuttonclick {Add {addhandler (okbuttonclickevent, value);} remove {removehandler (okbuttonclickevent, value );}}}
Or
Public partial class usercontrol1: usercontrol {public event routedeventhandler cancelbuttonclick ;}
The events are:
Protected void on_btnok_click (Object sender, routedeventargs e) {raiseevent (New routedeventargs (okbuttonclickevent ));}
And
Protected void on_btncancel_click (Object sender, routedeventargs e) {If (cancelbuttonclick! = NULL) {cancelbuttonclick (sender, e );}}
Write test cases:
Public void test1 () {// This. usercontrol11.addhandler (usercontrol1.buttonclickevent, new routedeventhandler (usercontrol11_okbuttonclick); this. usercontrol11.okbuttonclick + = new routedeventhandler (usercontrol11_okbuttonclick); this. usercontrol11.cancelbuttonclick + = new routedeventhandler (usercontrol11_cancelbuttonclick); button btnok = This. usercontrol11.findname ("btnok") as button; btnok. raiseevent (New routedeventargs (button. clickevent); button btncancel = This. usercontrol11.findname ("btncancel") as button; btncancel. raiseevent (New routedeventargs (button. clickevent); this. usercontrol11.okbuttonclick-= new routedeventhandler (usercontrol11_okbuttonclick); this. usercontrol11.cancelbuttonclick-= new routedeventhandler (usercontrol11_cancelbuttonclick);} private void usercontrol11_okbuttonclick (Object sender, routedeventargs e) {console. writeline ("usercontrol11_okbuttonclick");} private void usercontrol11_cancelbuttonclick (Object sender, routedeventargs e) {console. writeline ("usercontrol11_cancelbuttonclick ");}
ExampleCode: Wpfcontrollibrary1.zip