Silverlight learning notes-create a Silverlight custom control (2)-Event Response

Source: Internet
Author: User

After designing the appearance of the custom control, we will design the Event Response of the custom control. In this example, we will add the Click Event of the control.

1. Add events
Implementing events in the Silverlight control is usually the same as implementing events in the common Microsoft. NET Framework class: you only need to declare events in the control class, and then writeCodeThese events are triggered.
However, unlike events in Silverlight, Silverlight supports routing events. In siverlight, events can only be passed up, that is, the "bubble" operation. Route events are defined by routedeventhandler delegation and are handled by route events.ProgramReceives the routedeventargs object, which contains the source attribute of the object used to confirm that this event was triggered (if this event was initially triggered by a deep object in the visual tree, this attribute is different from the sender parameter passed to the event handler ). The click event of the built-in button control is a routing event, so the click event of simplebutton should also be a routing event.
The control class in mysilverbutton. CS must be modified to cause the routing click event. Now, we need to register a handler for the mouseleftbuttonup event in the mysilverbutton constructor. If at least one registered listener exists, this handler can trigger the click event. If the button-down event is prior to the button-up event, the traditional button control can only trigger the click event. To enableSource codeAs simple as possible. This logic is omitted in mysilverbutton.
The code to be added is as follows:

Public   Event Routedeventhandler click; // As stated earlier
This . Mouseleftbuttonup + =   New Mousebuttoneventhandler (simplebutton_mouseleftbuttonup ); // Add to Constructor
Void Simplebutton_mouseleftbuttonup ( Object Sender, mousebuttoneventargs E)
{
If (Click ! =   Null )
Click ( This , New Routedeventargs ());
}

 

The complete code is as follows:

Code
Using System;
Using System. net;
Using System. windows;
Using System. Windows. controls;
Using System. Windows. documents;
Using System. Windows. Ink;
Using System. Windows. input;
Using System. Windows. Media;
Using System. Windows. Media. animation;
Using System. Windows. shapes;

Namespace Mydesignbutton
{
Public   Class Mysilverbutton: contentcontrol
{
Public   Event Routedeventhandler click; // Add a click event

Public Mysilverbutton ()
{
This . Defaultstylekey =   Typeof (Mysilverbutton );

This. Mouseleftbuttonup+ = NewMousebuttoneventhandler (simplebutton_mouseleftbuttonup );//Add a click event

}

// Add a click event
Void Simplebutton_mouseleftbuttonup ( Object Sender, mousebuttoneventargs E)
{
If (Click ! =   Null )
Click ( This , New Routedeventargs ());
}
}
}

 

2. Test Event Response
To test the click event, we need to go back to the myslbutton project and modify the control declaration in page. XAML, as shown below:

< Custom: mysilverbutton X: Name = " Myfirstslbutton " Click = " Myfirstslbutton_click " >
</ Custom: mysilverbutton >

Then write the myfirstslbutton_click event handler in page. XAML. cs. The Code is as follows:

Private   Void Myfirstslbutton_click ( Object Sender, routedeventargs E)
{
System. Windows. browser. htmlpage. Window. Alert ( " Click Event takes effect! " );
}

 

After the project is generated and run, click button to generate a word containing "Click Event effective !" Warning box-to prove that this event has been raised and handled correctly:
Next article:
Silverlight learning notes -- create a Silverlight custom control (3) -- operation control internal Member
Go to: Silverlight Study Notes List

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.