FrameworkElement.TemplatedParent 屬性


取得這個項目的範本父代參考。Gets a reference to the template parent of this element. 若此項目不是透過範本建立,則這個屬性不相關。This property is not relevant if the element was not created through a template.

 property System::Windows::DependencyObject ^ TemplatedParent { System::Windows::DependencyObject ^ get(); };
public System.Windows.DependencyObject TemplatedParent { get; }
member this.TemplatedParent : System.Windows.DependencyObject
Public ReadOnly Property TemplatedParent As DependencyObject


此項目由其 FrameworkTemplateVisualTree 所建立。The element whose FrameworkTemplateVisualTree caused this element to be created. 這個值通常是 nullThis value is frequently null.


對於在應用程式標記或程式碼中建立的物件,通常會 null TemplatedParentTemplatedParent is frequently null for objects that are created in your application markup or code. 這是因為您是直接建立這些物件,而不是透過範本。This is because you create those objects directly, not via a template. 藉由從根流覽邏輯樹狀結構或一般名稱參考取得的物件參考,不是來自範本。Object references obtained by walking the logical tree from the root, or by typical name references, do not come from a template.

可能無法 null TemplatedParent 的情況包括:點擊測試、特定低層級輸入事件的事件處理、使用 VisualTreeHelper流覽視覺化樹狀結構,或使用列舉值,這可能會傳回來自範本的專案。Cases where TemplatedParent might not be null include operations such as hit-testing, event handling for certain low-level input events, walking the visual tree with VisualTreeHelper, or working with enumerators, which might return elements that came from templates. 另一種情況是,如果您特別針對現有的 FrameworkTemplate 呼叫 FindName,並使用傳回的物件。Another case is if you specifically call FindName against an existing FrameworkTemplate and are work with the returned object.

範本實際上是共用物件,其中範本的內容只會建立一次。Templates are actually shared objects, where the contents of the template are created only once. 因此,如果您取得來自範本之元素的物件參考,您可能會發現明顯的邏輯樹狀結構無法到達頁面根目錄。Therefore, if you obtain an object reference to an element that came from a template, you may find that the apparent logical tree does not reach to the page root. 若要將這類範本參考連接到頁面的邏輯樹狀結構,您應該取得 TemplatedParent 值,並繼續視需要流覽該專案樹狀結構。In order to connect such a template reference to the page's logical tree, you should get the TemplatedParent value and continue to navigate that element tree as desired.