// ----------------------------------------------------------------------- // // Copyright 2014 MIT Licence. See licence.md for more information. // // ----------------------------------------------------------------------- namespace Perspex.Controls { using System; using System.Collections; using System.Linq; using System.Reactive.Linq; public class TabControl : SelectingItemsControl { public static readonly PerspexProperty SelectedContentProperty = PerspexProperty.Register("SelectedContent"); private TabStrip tabStrip; public TabControl() { this.GetObservable(ItemsProperty).Subscribe(this.ItemsChanged); this.GetObservable(SelectedItemProperty).Skip(1).Subscribe(this.SelectedItemChanged); } protected override void OnTemplateApplied() { this.tabStrip = this.GetTemplateControls() .OfType() .FirstOrDefault(); if (this.tabStrip != null) { this.tabStrip.SelectedItem = this.SelectedItem; this.tabStrip.GetObservable(TabStrip.SelectedItemProperty).Skip(1).Subscribe(x => { this.SelectedItem = x; }); } } private void ItemsChanged(IEnumerable items) { if (items != null) { this.SelectedItem = items.OfType().FirstOrDefault(); } else { this.SelectedItem = null; } } private void SelectedItemChanged(object item) { this.SelectedItem = item; ContentControl content = item as ContentControl; if (content != null) { this.SetValue(SelectedContentProperty, content.Content); } else { this.SetValue(SelectedContentProperty, item); } } } }