(注:本文是《剖析WPF模板機制的內部實現》系列文章的第二篇,查看上一篇文章點這裏)html
2. ControlTemplatenode
最多見的ControlTemplate類型變量是Control.Template屬性,此外Control還覆寫了FrameworkElement的TemplateInternal何TemplateCache屬性:ide
//*****************Control********************
public static readonly DependencyProperty TemplateProperty = DependencyProperty.Register( "Template", typeof(ControlTemplate), typeof(Control), new FrameworkPropertyMetadata( (ControlTemplate) null, // default value FrameworkPropertyMetadataOptions.AffectsMeasure, new PropertyChangedCallback(OnTemplateChanged))); /// <summary> /// Template Property /// </summary> public ControlTemplate Template { get { return _templateCache; } set { SetValue(TemplateProperty, value); } } // Internal Helper so the FrameworkElement could see this property internal override FrameworkTemplate TemplateInternal { get { return Template; } } // Internal Helper so the FrameworkElement could see the template cache internal override FrameworkTemplate TemplateCache { get { return _templateCache; } set { _templateCache = (ControlTemplate) value; } } // Internal helper so FrameworkElement could see call the template changed virtual internal override void OnTemplateChangedInternal(FrameworkTemplate oldTemplate, FrameworkTemplate newTemplate) { OnTemplateChanged((ControlTemplate)oldTemplate, (ControlTemplate)newTemplate); } // Property invalidation callback invoked when TemplateProperty is invalidated private static void OnTemplateChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { Control c = (Control) d; StyleHelper.UpdateTemplateCache(c, (FrameworkTemplate) e.OldValue, (FrameworkTemplate) e.NewValue, TemplateProperty); }
能夠看到,TemplateInternal屬性返回的就是Template,而Template屬性返回的是_templateCache字段,_templateCache字段又是TemplateCache屬性的支撐字段,而TemplateCache屬性只在StyleHelper.UpdateTemplateCache()方法裏被修改過。這個方法的代碼以下:this
//*****************StyleHelper********************* internal static void UpdateTemplateCache( FrameworkElement fe, FrameworkTemplate oldTemplate, FrameworkTemplate newTemplate, DependencyProperty templateProperty) { DependencyObject d = fe;// Update the template cache fe.TemplateCache = newTemplate; // Do template property invalidations. Note that some of the invalidations may be callouts // that could turn around and query the template property on this node. Hence it is essential // to update the template cache before we do this operation. StyleHelper.DoTemplateInvalidations(fe, oldTemplate); // Now look for triggers that might want their EnterActions or ExitActions // to run immediately. StyleHelper.ExecuteOnApplyEnterExitActions(fe, null, newTemplate); }
這意味着每次更新Control.Template都會相應更新_templateCache,從而FrameworkElement.ApplyTemplate()讀取到的TemplateInternal的值也就是Control.Template的值。就這樣,Control類在沒法覆寫ApplyTemplate()方法的狀況下,實現了模板應用的多態性。值得一提的是,咱們後面將看到,這種模式已經成了FrameworkElement子類對虛屬性TemplateInternal實現多態的固定模式。另外,前面咱們提到只有4個FrameworkElement的子類覆寫了TemplateInternal屬性:Control、ContentPresenter、ItemsPresenter、Page,所以能夠指望在後面三種類裏面也能找到相似的TemplateInternal多態性實現機制。spa
其餘ControlTemplate類型的變量還有Page.Template,DataGrid.RowValidationErrorTemplate等。它們的模板機制與Control.Template大同小異,這裏就不一一贅述了。下一篇文章咱們將討論ItemsPanelTemplate類。code