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

要尋找的子項目名稱。

傳回

DependencyObject

要求的項目。 如果所要求名稱的項目不存在,則可能為 null

備註

WPF 中的範本具有獨立的命名範圍。 這是因為重複使用範本,而且當控制項的多個實例都具現化範本時,範本中定義的任何名稱都不能保持唯一。 呼叫 方法, GetTemplateChild 以傳回具現化後來自範本之物件的參考。 您無法使用 FrameworkElement.FindName 方法來尋找範本中的專案,因為 FrameworkElement.FindName 作用在較一般的範圍內,而且套用類別本身與具現化範本之間 ControlTemplate 沒有連線。

FrameworkTemplate.FindName 提供與這個方法相同的函式。 FrameworkTemplate.FindName 是公用的,而不是受保護的,而且會使用正確的名稱範圍考慮,讓它可以存取元素內的範本,並在其中尋找具名專案。 當您需要在其父控制項之外取得元素時,請使用 FrameworkTemplate.FindName

適用於

另請參閱