diff --git a/Perspex.Controls/Presenters/ContentPresenter.cs b/Perspex.Controls/Presenters/ContentPresenter.cs index 855591adef..3d9958cf6f 100644 --- a/Perspex.Controls/Presenters/ContentPresenter.cs +++ b/Perspex.Controls/Presenters/ContentPresenter.cs @@ -30,23 +30,21 @@ namespace Perspex.Controls.Presenters set { this.SetValue(ContentProperty, value); } } - protected override Size MeasureCore(Size availableSize) + protected override sealed void ApplyTemplate() { if (!this.createdChild) { this.CreateChild(); } + } + protected override Size MeasureCore(Size availableSize) + { return base.MeasureCore(availableSize); } protected override Size MeasureOverride(Size availableSize) { - if (!this.createdChild) - { - this.CreateChild(); - } - Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control; if (child != null) diff --git a/Perspex.Controls/Presenters/ItemsPresenter.cs b/Perspex.Controls/Presenters/ItemsPresenter.cs index bd4e7ead66..4ed3c5850b 100644 --- a/Perspex.Controls/Presenters/ItemsPresenter.cs +++ b/Perspex.Controls/Presenters/ItemsPresenter.cs @@ -41,13 +41,16 @@ namespace Perspex.Controls.Presenters set { this.SetValue(ItemsPanelProperty, value); } } - protected override Size MeasureOverride(Size availableSize) + protected override sealed void ApplyTemplate() { if (!this.createdPanel) { this.CreatePanel(); } + } + protected override Size MeasureOverride(Size availableSize) + { panel.Measure(availableSize); return panel.DesiredSize.Value; } diff --git a/Perspex.Controls/Primitives/TemplatedControl.cs b/Perspex.Controls/Primitives/TemplatedControl.cs index c9a046b083..207e231745 100644 --- a/Perspex.Controls/Primitives/TemplatedControl.cs +++ b/Perspex.Controls/Primitives/TemplatedControl.cs @@ -30,6 +30,28 @@ namespace Perspex.Controls.Primitives { } + protected sealed override void ApplyTemplate() + { + if (!this.templateApplied) + { + this.ClearVisualChildren(); + + if (this.Template != null) + { + this.Log().Debug( + "Creating template for {0} (#{1:x8})", + this.GetType().Name, + this.GetHashCode()); + + var child = this.Template.Build(this); + this.AddVisualChild(child); + this.OnTemplateApplied(); + } + + this.templateApplied = true; + } + } + protected override Size ArrangeOverride(Size finalSize) { Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control; @@ -47,11 +69,6 @@ namespace Perspex.Controls.Primitives protected override Size MeasureOverride(Size availableSize) { - if (!this.templateApplied) - { - this.ApplyTemplate(); - } - Control child = ((IVisual)this).VisualChildren.SingleOrDefault() as Control; if (child != null) @@ -86,24 +103,5 @@ namespace Perspex.Controls.Primitives protected virtual void OnTemplateApplied() { } - - private void ApplyTemplate() - { - this.ClearVisualChildren(); - - if (this.Template != null) - { - this.Log().Debug( - "Creating template for {0} (#{1:x8})", - this.GetType().Name, - this.GetHashCode()); - - var child = this.Template.Build(this); - this.AddVisualChild(child); - this.OnTemplateApplied(); - } - - this.templateApplied = true; - } } } diff --git a/Perspex.Layout/Layoutable.cs b/Perspex.Layout/Layoutable.cs index 3cf9cd9a6c..30e9e43797 100644 --- a/Perspex.Layout/Layoutable.cs +++ b/Perspex.Layout/Layoutable.cs @@ -196,6 +196,10 @@ namespace Perspex.Layout property.Changed.Subscribe(AffectsMeasureInvalidate); } + protected virtual void ApplyTemplate() + { + } + protected virtual void ArrangeCore(Rect finalRect) { if (this.IsVisible) @@ -259,6 +263,8 @@ namespace Perspex.Layout { if (this.IsVisible) { + this.ApplyTemplate(); + var constrained = LayoutHelper.ApplyLayoutConstraints(this, availableSize) .Deflate(this.Margin);