FrameworkElement.ApplyTemplate FrameworkElement.ApplyTemplate FrameworkElement.ApplyTemplate FrameworkElement.ApplyTemplate Method


在必要时生成当前模板的可视化树,然后返回一个值,该值指示可视化树是否由此调用重新生成。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.

ApplyTemplateWPF 框架级别布局系统对每个度量值调用。ApplyTemplate 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. OnApplyTemplate模板完全生成并附加到逻辑树后, 调用。OnApplyTemplate is called after the template is completely generated and attached to the logical tree.