Browse Source

TabStrip now selects on click.

pull/4/head
Steven Kirk 12 years ago
parent
commit
69a0a81cdd
  1. 11
      Perspex/Controls/TabItem.cs
  2. 32
      Perspex/Controls/TabStrip.cs
  3. 16
      Perspex/VisualExtensions.cs
  4. 1
      TestApplication/Program.cs

11
Perspex/Controls/TabItem.cs

@ -8,8 +8,19 @@ namespace Perspex.Controls
{
public class TabItem : HeaderedContentControl
{
public static readonly PerspexProperty<bool> IsSelectedProperty =
PerspexProperty.Register<TabItem, bool>("IsSelected");
public TabItem()
{
this.AddPseudoClass(IsSelectedProperty, ":selected");
AffectsRender(IsSelectedProperty);
}
public bool IsSelected
{
get { return this.GetValue(IsSelectedProperty); }
set { this.SetValue(IsSelectedProperty, value); }
}
}
}

32
Perspex/Controls/TabStrip.cs

@ -4,12 +4,10 @@
// </copyright>
// -----------------------------------------------------------------------
using Perspex.Input;
namespace Perspex.Controls
{
using System;
using System.Collections.Generic;
using System.Linq;
public class TabStrip : ItemsControl
{
private static readonly ItemsPanelTemplate PanelTemplate = new ItemsPanelTemplate(
@ -23,5 +21,31 @@ namespace Perspex.Controls
ItemsPanelProperty.OverrideDefaultValue(typeof(TabStrip), PanelTemplate);
ItemTemplateProperty.OverrideDefaultValue(typeof(TabStrip), TabTemplate);
}
public TabStrip()
{
this.PointerPressed += this.OnPointerPressed;
}
private void OnPointerPressed(object sender, PointerEventArgs e)
{
IVisual source = (IVisual)e.Source;
ContentPresenter presenter = source.GetVisualAncestor<ContentPresenter>();
if (presenter != null)
{
TabItem item = presenter.TemplatedParent as TabItem;
if (item != null && item.TemplatedParent == this)
{
item.IsSelected = true;
foreach (var i in item.GetVisualSiblings<TabItem>())
{
i.IsSelected = false;
}
}
}
}
}
}

16
Perspex/VisualExtensions.cs

@ -124,5 +124,21 @@ namespace Perspex
}
}
}
public static IEnumerable<T> GetVisualSiblings<T>(this IVisual visual)
{
IVisual parent = visual.VisualParent;
if (parent != null)
{
foreach (T sibling in parent.VisualChildren.OfType<T>())
{
if ((IVisual)sibling != visual)
{
yield return sibling;
}
}
}
}
}
}

1
TestApplication/Program.cs

@ -77,6 +77,7 @@ namespace TestApplication
new TabItem
{
Header = "Tab 1",
IsSelected = true,
},
new TabItem
{

Loading…
Cancel
Save