// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Collections.Generic; using System.Linq; using Perspex.Media; public abstract class TemplatedControl : Control { public static readonly PerspexProperty> TemplateProperty = PerspexProperty.Register>("Template"); private Visual visualChild; public TemplatedControl() { this.Template = owner => this.DefaultTemplate(); } public Func Template { get { return this.GetValue(TemplateProperty); } set { this.SetValue(TemplateProperty, value); } } public override IEnumerable VisualChildren { get { var template = this.Template; if (this.visualChild == null && template != null) { this.visualChild = template(this); this.visualChild.VisualParent = this; } return Enumerable.Repeat(this.visualChild, this.visualChild != null ? 1 : 0); } } public sealed override void Render(IDrawingContext context) { } protected abstract Visual DefaultTemplate(); } }