// ----------------------------------------------------------------------- // // 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.Input; using Perspex.Layout; public class DropDown : SelectingItemsControl, IContentControl, ILogical { public static readonly PerspexProperty ContentProperty = ContentControl.ContentProperty.AddOwner(); public static readonly PerspexProperty HorizontalContentAlignmentProperty = ContentControl.HorizontalContentAlignmentProperty.AddOwner(); public static readonly PerspexProperty VerticalContentAlignmentProperty = ContentControl.VerticalContentAlignmentProperty.AddOwner(); public static readonly PerspexProperty IsDropDownOpenProperty = PerspexProperty.Register("IsDropDownOpen"); private PerspexReadOnlyListView logicalChildren = new PerspexReadOnlyListView(); public DropDown() { this.GetObservableWithHistory(ContentProperty).Subscribe(this.SetContentParent); this.Bind(ContentProperty, this.GetObservable(DropDown.SelectedItemProperty)); } 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); } } public bool IsDropDownOpen { get { return this.GetValue(IsDropDownOpenProperty); } set { this.SetValue(IsDropDownOpenProperty, value); } } IPerspexReadOnlyList ILogical.LogicalChildren { get { return this.logicalChildren; } } protected override void OnPointerPressed(PointerPressEventArgs e) { if (!this.IsDropDownOpen) { this.IsDropDownOpen = true; } } protected override void OnTemplateApplied() { var container = this.GetTemplateChild("container"); ((IItemsPanel)container).ChildLogicalParent = this; this.logicalChildren.Source = ((ILogical)container).LogicalChildren; } 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; } } } }