// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using Perspex.Animation; using Perspex.Controls.Primitives; using Perspex.Controls.Templates; /// /// A tab control that displays a tab strip along with the content of the selected tab. /// public class TabControl : SelectingItemsControl { /// /// Defines the property. /// public static readonly PerspexProperty SelectedContentProperty = PerspexProperty.Register("SelectedContent"); /// /// Defines the property. /// public static readonly PerspexProperty SelectedTabProperty = PerspexProperty.Register("SelectedTab"); /// /// Defines the property. /// public static readonly PerspexProperty TransitionProperty = Deck.TransitionProperty.AddOwner(); /// /// Initializes static members of the class. /// static TabControl() { AutoSelectProperty.OverrideDefaultValue(true); FocusableProperty.OverrideDefaultValue(false); SelectedIndexProperty.Changed.AddClassHandler(x => x.SelectedIndexChanged); } /// /// Gets the content of the selected tab. /// public object SelectedContent { get { return this.GetValue(SelectedContentProperty); } private set { this.SetValue(SelectedContentProperty, value); } } /// /// Gets the as a . /// public TabItem SelectedTab { get { return this.GetValue(SelectedTabProperty); } private set { this.SetValue(SelectedTabProperty, value); } } /// /// Gets or sets the transition to use when switching tabs. /// public IPageTransition Transition { get { return this.GetValue(TransitionProperty); } set { this.SetValue(TransitionProperty, value); } } /// protected override void OnTemplateApplied() { base.OnTemplateApplied(); var deck = this.GetTemplateChild("deck"); ((IReparentingControl)deck.Presenter.Panel).ReparentLogicalChildren( this, this.LogicalChildren); } /// /// Called when the property changes. /// /// The event args. private void SelectedIndexChanged(PerspexPropertyChangedEventArgs e) { if ((int)e.NewValue != -1) { var item = this.SelectedItem as IContentControl; var content = item?.Content ?? item; this.SelectedTab = item as TabItem; this.SelectedContent = content; } } } }