FrameworkElement.GetTemplateChild(String) FrameworkElement.GetTemplateChild(String) FrameworkElement.GetTemplateChild(String) FrameworkElement.GetTemplateChild(String) Method


傳回具現化 ControlTemplate 視覺化樹狀結構中的具名項目。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


String String String String

要尋找的子項目名稱。Name of the child to find.


要求的項目。The requested element. 如果所要求名稱的項目不存在,則可能為 nullMay be null if no element of the requested name exists.


中的範本WPFWPF有獨立的名稱範圍。Templates in WPFWPF 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. 呼叫GetTemplateChild方法,以傳回來自範本之後它會具現化, 物件的參考。Call the GetTemplateChild method to return references to objects that come from the template after it is instantiated. 您無法使用FrameworkElement.FindName方法來尋找範本中的項目,因為FrameworkElement.FindName作用中的多個一般範圍,以及之間沒有連線ControlTemplate類別本身和具現化的範本,一旦套用。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 提供這個方法相同的功能。FrameworkTemplate.FindName supplies the same function as this method. FrameworkTemplate.FindName 已受到保護而不是公用,並使用正確名稱範圍,使其得以存取項目] 和 [尋找名為其內的項目內的範本考量。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. 使用FrameworkTemplate.FindName當您需要取得其父控制項之外的項目。Use FrameworkTemplate.FindName when you need to get an element outside of its parent control.