FrameworkElement.ApplyTemplate 方法


在必要时生成当前模板的可视化树,然后返回一个值,该值指示可视化树是否由此调用重新生成。Builds the current template's visual tree if necessary, and returns a value that indicates whether the visual tree was rebuilt by this call.

 bool ApplyTemplate();
public bool ApplyTemplate ();
member this.ApplyTemplate : unit -> bool
Public Function ApplyTemplate () As Boolean


如果在树中添加了视觉对象,则返回 true;否则返回 falsetrue if visuals were added to the tree; returns false otherwise.


应用程序可以调用此方法以确保元素的可视化树已完成。Applications can call this method to guarantee that the visual tree of an element is complete. 如果代码要检查树中的子元素,则此保证步骤可能是必需的。This guarantee step might be necessary if code will be checking the child elements in the tree. 对于应用程序中的典型元素逻辑,不需要调用 ApplyTemplate,因为模板会自动应用到其生存期中相应点的元素。For typical element logic within applications, calling ApplyTemplate is not necessary, because templates will be applied to elements at an appropriate point in their lifetimes automatically.

WPF 框架级别布局系统对每个度量值调用 ApplyTemplateApplyTemplate is called on every Measure pass by the WPF framework-level layout system.

FrameworkElement 派生类可以使用 OnApplyTemplate 类处理程序,以便在显式调用此方法或布局系统的情况下获得通知。FrameworkElement derived classes can use the OnApplyTemplate class handler to be notified of cases where this method was called explicitly, or by the layout system. 在完整生成模板并将其附加到逻辑树后,调用 OnApplyTemplateOnApplyTemplate is called after the template is completely generated and attached to the logical tree.