How to: access controls in a control Template

Source: Internet
Author: User

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;
}
}

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.