24 changed files with 501 additions and 98 deletions
@ -0,0 +1,37 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="ControlExtensions.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex |
||||
|
{ |
||||
|
using System; |
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Perspex.Controls; |
||||
|
using Perspex.Styling; |
||||
|
|
||||
|
public static class ControlExtensions |
||||
|
{ |
||||
|
public static IEnumerable<Control> GetTemplateControls(this ITemplatedControl control) |
||||
|
{ |
||||
|
return GetTemplateControls(control, (IVisual)control); |
||||
|
} |
||||
|
|
||||
|
public static IEnumerable<Control> GetTemplateControls(ITemplatedControl templated, IVisual parent) |
||||
|
{ |
||||
|
IVisual visual = parent as IVisual; |
||||
|
|
||||
|
foreach (IVisual child in visual.VisualChildren.OfType<Control>().Where(x => x.TemplatedParent == templated)) |
||||
|
{ |
||||
|
yield return (Control)child; |
||||
|
|
||||
|
foreach (IVisual grandchild in GetTemplateControls(templated, child)) |
||||
|
{ |
||||
|
yield return (Control)grandchild; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="Controls.cs" company="Steven Kirk">
|
||||
|
// Copyright 2013 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
|
||||
|
namespace Perspex.Controls |
||||
|
{ |
||||
|
public class Controls : PerspexList<Control> |
||||
|
{ |
||||
|
public Controls() |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
public Controls(IEnumerable<Control> items) |
||||
|
: base(items) |
||||
|
{ |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,20 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="SelectingItemsControl.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Controls |
||||
|
{ |
||||
|
public class SelectingItemsControl : ItemsControl |
||||
|
{ |
||||
|
public static readonly PerspexProperty<object> SelectedItemProperty = |
||||
|
PerspexProperty.Register<SelectingItemsControl, object>("SelectedItem"); |
||||
|
|
||||
|
public object SelectedItem |
||||
|
{ |
||||
|
get { return this.GetValue(SelectedItemProperty); } |
||||
|
set { this.SetValue(SelectedItemProperty, value); } |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,71 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <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); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,54 @@ |
|||||
|
// -----------------------------------------------------------------------
|
||||
|
// <copyright file="TabControlStyle.cs" company="Steven Kirk">
|
||||
|
// Copyright 2014 MIT Licence. See licence.md for more information.
|
||||
|
// </copyright>
|
||||
|
// -----------------------------------------------------------------------
|
||||
|
|
||||
|
namespace Perspex.Themes.Default |
||||
|
{ |
||||
|
using System.Linq; |
||||
|
using System.Reactive.Linq; |
||||
|
using Perspex.Controls; |
||||
|
using Perspex.Styling; |
||||
|
|
||||
|
public class TabControlStyle : Styles |
||||
|
{ |
||||
|
public TabControlStyle() |
||||
|
{ |
||||
|
this.AddRange(new[] |
||||
|
{ |
||||
|
new Style(x => x.OfType<TabControl>()) |
||||
|
{ |
||||
|
Setters = new[] |
||||
|
{ |
||||
|
new Setter(TabControl.TemplateProperty, ControlTemplate.Create<TabControl>(this.Template)), |
||||
|
}, |
||||
|
}, |
||||
|
}); |
||||
|
} |
||||
|
|
||||
|
private Control Template(TabControl control) |
||||
|
{ |
||||
|
return new Grid |
||||
|
{ |
||||
|
RowDefinitions = new RowDefinitions |
||||
|
{ |
||||
|
new RowDefinition(GridLength.Auto), |
||||
|
new RowDefinition(new GridLength(1, GridUnitType.Star)), |
||||
|
}, |
||||
|
Children = new Controls |
||||
|
{ |
||||
|
new TabStrip |
||||
|
{ |
||||
|
[~TabStrip.ItemsProperty] = control[~TabControl.ItemsProperty], |
||||
|
}, |
||||
|
new ContentPresenter |
||||
|
{ |
||||
|
[~ContentPresenter.ContentProperty] = control[~TabControl.SelectedContentProperty], |
||||
|
[Grid.RowProperty] = 1, |
||||
|
} |
||||
|
} |
||||
|
}; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue