แก้ไข

แชร์ผ่าน


FrameworkElement.GetTemplateChild(String) Method

Definition

Returns the named element in the visual tree of an instantiated ControlTemplate.

protected public:
 System::Windows::DependencyObject ^ GetTemplateChild(System::String ^ childName);
protected internal System.Windows.DependencyObject GetTemplateChild (string childName);
member this.GetTemplateChild : string -> System.Windows.DependencyObject
Protected Friend Function GetTemplateChild (childName As String) As DependencyObject

Parameters

childName
String

Name of the child to find.

Returns

The requested element. May be null if no element of the requested name exists.

Remarks

Templates in WPF have a self-contained namescope. This is because templates are re-used, and any name defined in a template cannot remain unique when multiple instances of a control each instantiate its template. Call the GetTemplateChild method to return references to objects that come from the template after it is instantiated. You cannot use the FrameworkElement.FindName method to find items from templates because FrameworkElement.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.

FrameworkTemplate.FindName supplies the same function as this method. FrameworkTemplate.FindName is public instead of protected, and it uses correct name-scoping considerations that allow it to access the template within an element and find named items within it. Use FrameworkTemplate.FindName when you need to get an element outside of its parent control.

Applies to

See also