// Copyright (c) The Perspex Project. All rights reserved. // Licensed under the MIT license. See licence.md file in the project root for full license information. using Perspex.Animation; using Perspex.Controls.Presenters; using Perspex.Controls.Primitives; using Perspex.Controls.Templates; namespace Perspex.Controls { /// /// A tab control that displays a tab strip along with the content of the selected tab. /// public class TabControl : SelectingItemsControl, IReparentingHost { /// /// Defines the property. /// public static readonly PerspexProperty SelectedTabProperty = PerspexProperty.Register("SelectedTab"); /// /// Defines the property. /// public static readonly PerspexProperty TransitionProperty = Deck.TransitionProperty.AddOwner(); private static readonly IMemberSelector s_contentSelector = new FuncMemberSelector(SelectContent); /// /// Initializes static members of the class. /// static TabControl() { AutoSelectProperty.OverrideDefaultValue(true); FocusableProperty.OverrideDefaultValue(false); SelectedIndexProperty.Changed.AddClassHandler(x => x.SelectedIndexChanged); } /// /// Gets an that selects the content of a . /// public IMemberSelector ContentSelector { get { return s_contentSelector; } } /// /// Gets the as a . /// public TabItem SelectedTab { get { return GetValue(SelectedTabProperty); } private set { SetValue(SelectedTabProperty, value); } } /// /// Gets or sets the transition to use when switching tabs. /// public IPageTransition Transition { get { return GetValue(TransitionProperty); } set { SetValue(TransitionProperty, value); } } /// /// 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 DeckPresenter; } /// /// Selects the content of a tab item. /// /// The tab item. /// The content. private static object SelectContent(object o) { var content = o as IContentControl; if (content != null) { return content.Content; } else { return o; } } /// /// Called when the property changes. /// /// The event args. private void SelectedIndexChanged(PerspexPropertyChangedEventArgs e) { if ((int)e.NewValue != -1) { var item = SelectedItem as IContentControl; var content = item?.Content ?? item; SelectedTab = item as TabItem; } } } }