Assume that the button control applies the following control template:
<Controltemplate X: Key = "startactivity" targettype = "button">
<Grid width = "Auto" Height = "62" margin = "0, 0, 0">
<Textblock Height = "0" margin = "0.5," verticalignment = "bottom" text = "" textwrapping = "Wrap" X: Name = "tblabel" rendertransformorigin =, 0.5 "horizontalalignment =" center "foreground =" # ff0507fa ">
</Textblock>
</GRID>
</Controltemplate>
If you wantCodeAccess the textblock control named tblabel in the template. How do I write code?
The control base class has a gettemplatechild method, but this method is of the protected type. Therefore, we must inherit the base class and overload onapplytemplate to call this method, as shown below:
Public class activitycontrol: button
{
Public override void onapplytemplate ()
{
Base. onapplytemplate ();
// Get the textblock control from template
Textblock label = gettemplatechild ("tblabel") as textblock;
}
}