A cross-platform UI framework for .NET
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

71 lines
2.1 KiB

// -----------------------------------------------------------------------
// <copyright file="TabControl.cs" company="Steven Kirk">
// Copyright 2014 MIT Licence. See licence.md for more information.
// </copyright>
// -----------------------------------------------------------------------
namespace Perspex.Controls
{
using System;
using System.Collections;
using System.Linq;
using System.Reactive.Linq;
public class TabControl : SelectingItemsControl
{
public static readonly PerspexProperty<object> SelectedContentProperty =
PerspexProperty.Register<TabControl, object>("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<TabStrip>()
.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<object>().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);
}
}
}
}