// -----------------------------------------------------------------------
//
// Copyright 2015 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;
///
/// Displays according to a .
///
public class ContentControl : TemplatedControl, IContentControl, IReparentingHost
{
///
/// Defines the property.
///
public static readonly PerspexProperty ContentProperty =
PerspexProperty.Register(nameof(Content));
///
/// Defines the property.
///
public static readonly PerspexProperty HorizontalContentAlignmentProperty =
PerspexProperty.Register(nameof(HorizontalContentAlignment));
///
/// Defines the property.
///
public static readonly PerspexProperty VerticalContentAlignmentProperty =
PerspexProperty.Register(nameof(VerticalContentAlignment));
///
/// Initializes a new instance of the class.
///
public ContentControl()
{
}
///
/// Gets or sets the content to display.
///
public object Content
{
get { return this.GetValue(ContentProperty); }
set { this.SetValue(ContentProperty, value); }
}
///
/// Gets the presenter from the control's template.
///
public ContentPresenter Presenter
{
get;
private set;
}
///
/// Gets or sets the horizontal alignment of the content within the control.
///
public HorizontalAlignment HorizontalContentAlignment
{
get { return this.GetValue(HorizontalContentAlignmentProperty); }
set { this.SetValue(HorizontalContentAlignmentProperty, value); }
}
///
/// Gets or sets the vertical alignment of the content within the control.
///
public VerticalAlignment VerticalContentAlignment
{
get { return this.GetValue(VerticalContentAlignmentProperty); }
set { this.SetValue(VerticalContentAlignmentProperty, value); }
}
///
/// Gets a writeable logical children collection from the host.
///
IPerspexList IReparentingHost.LogicalChildren => this.LogicalChildren;
///
/// Asks the control whether it wants to reparent the logical children of the specified
/// control.
///
/// The control.
///
/// True if the control wants to reparent its logical children otherwise false.
///
bool IReparentingHost.WillReparentChildrenOf(IControl control)
{
return control is IContentPresenter && control.TemplatedParent == this;
}
///
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");
}
}
}