語言

FrameworkElement.GetTemplateChild(String) 方法

定義

回傳實例 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

參數

childName
String

要找的孩子名字。

傳回

被要求的元素。 如果請求名稱中沒有任何元素,可能會有 null 。

備註

WPF 中的範本有自包含的名稱範圍。 這是因為模板會重複使用,且當多個控制項實例各自實例化其範本時,模板中定義的任何名稱都無法保持唯一。 呼叫 GetTemplateChild 該方法,回傳範本實例化後的物件參考。 你無法用這個 FrameworkElement.FindName 方法從範本中尋找項目,因為它 FrameworkElement.FindName 作用範圍更廣,且套用後類別本身與實例化的範本之間沒有連結 ControlTemplate 。

FrameworkTemplate.FindName 提供與此方法相同的功能。 FrameworkTemplate.FindName 是公開的,而非受保護的,並且它使用正確的命名範圍考量,使它能在元素中存取範本並找到其中的命名項目。 當你需要讓元素脫離父體控制時使用 FrameworkTemplate.FindName 。

適用於

另請參閱