In the above custom control mysilverbutton, we have two internal members: rectangle (bodyelement) and textblock (buttoncaption). To operate on them, we need to use the gettemplatechild function to achieve our goal.
Here, we modify the text attribute value of textblock. Join Code To the simplebutton_mouseleftbuttonup event Void Simplebutton_mouseleftbuttonup ( Object Sender, mousebuttoneventargs E)
{
If (Click ! = Null )
{
Click ( This , New Routedeventargs ());
Textblock bgtextblock = (Sender As Mysilverbutton ).( " Buttoncaption " ) As Textblock;
Bgtextblock. Text = " Modify display text " ;
}
}
The complete code of mysilverbutton. CS is as follows: 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 ());
Textblock bgtextblock = (Sender As Mysilverbutton ).( " Buttoncaption " ) As Textblock;
Bgtextblock. Text = " Modify display text " ;
}
}
}
}
Re-Generate and return to the myslbutton project to run the test. You can see the result.
Next article:
Silverlight learning notes -- create a Silverlight custom control (4) -- add custom attributes
Go to: Silverlight Study Notes List