// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using Perspex.Collections; using Perspex.Controls.Presenters; using Perspex.Controls.Primitives; using Perspex.Controls.Templates; using Perspex.Layout; public class ContentControl : TemplatedControl, IContentControl, ILogical { public static readonly PerspexProperty ContentProperty = PerspexProperty.Register("Content"); public static readonly PerspexProperty HorizontalContentAlignmentProperty = PerspexProperty.Register("HorizontalContentAlignment"); public static readonly PerspexProperty VerticalContentAlignmentProperty = PerspexProperty.Register("VerticalContentAlignment"); private PerspexReadOnlyListView logicalChildren = new PerspexReadOnlyListView(); public ContentControl() { } public object Content { get { return this.GetValue(ContentProperty); } set { this.SetValue(ContentProperty, value); } } public ContentPresenter Presenter { get; private set; } public HorizontalAlignment HorizontalContentAlignment { get { return this.GetValue(HorizontalContentAlignmentProperty); } set { this.SetValue(HorizontalContentAlignmentProperty, value); } } public VerticalAlignment VerticalContentAlignment { get { return this.GetValue(VerticalContentAlignmentProperty); } set { this.SetValue(VerticalContentAlignmentProperty, value); } } IPerspexReadOnlyList ILogical.LogicalChildren { get { return this.logicalChildren; } } protected override void OnTemplateApplied() { // We allow ContentControls without ContentPresenters in the template. This can be // useful for e.g. a simple ToggleButton that displays an image. There's no need to // have a ContentPresenter in the visual tree for that. this.Presenter = this.FindTemplateChild("contentPresenter"); this.logicalChildren.Source = ((ILogical)this.Presenter)?.LogicalChildren; } } }