// -----------------------------------------------------------------------
//
// 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.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 SingleItemPerspexList logicalChild = new SingleItemPerspexList();
private ContentPresenter presenter;
private IDisposable presenterSubscription;
public ContentControl()
{
this.GetObservableWithHistory(ContentProperty).Subscribe(this.SetContentParent);
}
public object Content
{
get { return this.GetValue(ContentProperty); }
set { this.SetValue(ContentProperty, value); }
}
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); }
}
IReadOnlyPerspexList ILogical.LogicalChildren
{
get { return this.logicalChild; }
}
protected override void OnTemplateApplied()
{
if (this.presenterSubscription != null)
{
this.presenterSubscription.Dispose();
this.presenterSubscription = null;
}
this.presenter = this.FindTemplateChild("contentPresenter");
if (this.presenter != null)
{
this.presenterSubscription = this.presenter.ChildObservable
.Subscribe(x => this.logicalChild.SingleItem = x);
}
}
private void SetContentParent(Tuple change)
{
var control1 = change.Item1 as Control;
var control2 = change.Item2 as Control;
if (control1 != null)
{
control1.Parent = null;
}
if (control2 != null)
{
control2.Parent = this;
}
}
}
}