A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

109 lines
4.1 KiB

// -----------------------------------------------------------------------
// <copyright file="ContentControl.cs" company="Steven Kirk">
// Copyright 2015 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
using Perspex.Collections;
using Perspex.Controls.Presenters;
using Perspex.Controls.Primitives;
using Perspex.Controls.Templates;
using Perspex.Layout;
/// <summary>
/// Displays <see cref="Content"/> according to a <see cref="DataTemplate"/>.
/// </summary>
public class ContentControl : TemplatedControl, IContentControl, IReparentingHost
{
/// <summary>
/// Defines the <see cref="Content"/> property.
/// </summary>
public static readonly PerspexProperty<object> ContentProperty =
PerspexProperty.Register<ContentControl, object>(nameof(Content));
/// <summary>
/// Defines the <see cref="HorizontalContentAlignment"/> property.
/// </summary>
public static readonly PerspexProperty<HorizontalAlignment> HorizontalContentAlignmentProperty =
PerspexProperty.Register<ContentControl, HorizontalAlignment>(nameof(HorizontalContentAlignment));
/// <summary>
/// Defines the <see cref="VerticalContentAlignment"/> property.
/// </summary>
public static readonly PerspexProperty<VerticalAlignment> VerticalContentAlignmentProperty =
PerspexProperty.Register<ContentControl, VerticalAlignment>(nameof(VerticalContentAlignment));
/// <summary>
/// Initializes a new instance of the <see cref="ContentControl"/> class.
/// </summary>
public ContentControl()
{
}
/// <summary>
/// Gets or sets the content to display.
/// </summary>
public object Content
{
get { return this.GetValue(ContentProperty); }
set { this.SetValue(ContentProperty, value); }
}
/// <summary>
/// Gets the presenter from the control's template.
/// </summary>
public ContentPresenter Presenter
{
get;
private set;
}
/// <summary>
/// Gets or sets the horizontal alignment of the content within the control.
/// </summary>
public HorizontalAlignment HorizontalContentAlignment
{
get { return this.GetValue(HorizontalContentAlignmentProperty); }
set { this.SetValue(HorizontalContentAlignmentProperty, value); }
}
/// <summary>
/// Gets or sets the vertical alignment of the content within the control.
/// </summary>
public VerticalAlignment VerticalContentAlignment
{
get { return this.GetValue(VerticalContentAlignmentProperty); }
set { this.SetValue(VerticalContentAlignmentProperty, value); }
}
/// <summary>
/// Gets a writeable logical children collection from the host.
/// </summary>
IPerspexList<ILogical> IReparentingHost.LogicalChildren => this.LogicalChildren;
/// <summary>
/// Asks the control whether it wants to reparent the logical children of the specified
/// control.
/// </summary>
/// <param name="control">The control.</param>
/// <returns>
/// True if the control wants to reparent its logical children otherwise false.
/// </returns>
bool IReparentingHost.WillReparentChildrenOf(IControl control)
{
return control is IContentPresenter && control.TemplatedParent == this;
}
/// <inheritdoc/>
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>("contentPresenter");
}
}
}