共用方式為


Control.GetTemplateChild Method

Microsoft Silverlight will reach end of support after October 2021. Learn more.

Retrieves the named element in the instantiated ControlTemplate visual tree.

Namespace:  System.Windows.Controls
Assembly:  System.Windows (in System.Windows.dll)

Syntax

'Declaration
Protected Function GetTemplateChild ( _
    childName As String _
) As DependencyObject
protected DependencyObject GetTemplateChild(
    string childName
)

Parameters

Return Value

Type: System.Windows.DependencyObject
The named element from the template, if found. Can return nulla null reference (Nothing in Visual Basic) if no element with name childName was found in the template.

Remarks

Templates in Silverlight have a self-contained namescope. This is because templates are re-used, and any name defined in a template cannot remain unique once multiple instances of a control each instantiate their template. Call GetTemplateChild in order to return references to objects that come from the template after it is instantiated. You cannot use FindName to find items from templates because FindName acts in a more general scope, and there is no connection between the ControlTemplate class itself and the instantiated template once it is applied.

Version Information

Silverlight

Supported in: 5, 4, 3

Silverlight for Windows Phone

Supported in: Windows Phone OS 7.1, Windows Phone OS 7.0

Platforms

For a list of the operating systems and browsers that are supported by Silverlight, see Supported Operating Systems and Browsers.