|
/* PivotItemEx.cs |
|
* ============== |
|
* Extensions to Microsoft.Phone.Controls.PivotItem |
|
* Pierre BELIN, 2013-14 <pierre@ree7.fr> |
|
* |
|
* Licensed under the MS-PL |
|
*/ |
|
|
|
using Microsoft.Phone.Controls; |
|
using System.Collections.Generic; |
|
using System.Windows; |
|
|
|
namespace ree7.Utils.Controls |
|
{ |
|
/// <summary> |
|
/// Extensions to Microsoft.Phone.Controls.PivotItem |
|
/// </summary> |
|
public static class PivotItemEx |
|
{ |
|
/// <summary> |
|
/// Holds metadata to properly restore the PivotItems |
|
/// </summary> |
|
private class PivotItemMetadata |
|
{ |
|
public Pivot Parent { get; set; } |
|
public int Position { get; set; } |
|
} |
|
|
|
private static List<Pivot> managedPivots = new List<Pivot>(); |
|
private static Dictionary<PivotItem, PivotItemMetadata> disabledItems = new Dictionary<PivotItem, PivotItemMetadata>(); |
|
|
|
public static bool GetIsDisplayed(DependencyObject obj) |
|
{ |
|
return (bool)obj.GetValue(IsDisplayedProperty); |
|
} |
|
|
|
public static void SetIsDisplayed(DependencyObject obj, bool value) |
|
{ |
|
obj.SetValue(IsDisplayedProperty, value); |
|
} |
|
|
|
// Using a DependencyProperty as the backing store for IsDisplayed. This enables animation, styling, binding, etc... |
|
public static readonly DependencyProperty IsDisplayedProperty = |
|
DependencyProperty.RegisterAttached("IsDisplayed", typeof(bool), typeof(PivotItemEx), new PropertyMetadata(true, OnIsDisplayedChanged)); |
|
|
|
private static void OnIsDisplayedChanged(DependencyObject obj, DependencyPropertyChangedEventArgs a) |
|
{ |
|
bool value = (bool)a.NewValue; |
|
|
|
// Only act when value turns false by removing the PivotItem from its parent |
|
if (value == false) |
|
{ |
|
PivotItem source = obj as PivotItem; |
|
if (obj == null) return; |
|
|
|
Pivot pivot = source.Parent as Pivot; |
|
if (pivot == null) return; |
|
|
|
// Store the pivot metadata |
|
PivotItemMetadata meta = new PivotItemMetadata(); |
|
meta.Parent = pivot; |
|
meta.Position = pivot.Items.IndexOf(source); |
|
disabledItems[source] = meta; |
|
|
|
Deployment.Current.Dispatcher.BeginInvoke(() => |
|
{ |
|
pivot.Items.Remove(source); |
|
}); |
|
} |
|
else |
|
{ |
|
PivotItem source = obj as PivotItem; |
|
if (obj == null) return; |
|
|
|
if (disabledItems.ContainsKey(source)) |
|
{ |
|
PivotItemMetadata meta = disabledItems[source]; |
|
Pivot parent = meta.Parent; |
|
|
|
disabledItems.Remove(source); |
|
if (CheckPivotStillUsed(parent) == false) |
|
{ |
|
UnsubscribePivot(parent); |
|
} |
|
|
|
Deployment.Current.Dispatcher.BeginInvoke(() => |
|
{ |
|
if (meta.Position > parent.Items.Count) |
|
{ |
|
parent.Items.Add(source); |
|
} |
|
else |
|
{ |
|
parent.Items.Insert(meta.Position, source); |
|
} |
|
}); |
|
} |
|
} |
|
} |
|
|
|
private static void OnManagedPivotUnloaded(object sender, RoutedEventArgs e) |
|
{ |
|
UnsubscribePivot((Pivot)sender); |
|
} |
|
|
|
private static bool CheckPivotStillUsed(Pivot p) |
|
{ |
|
foreach (var meta in disabledItems.Values) |
|
{ |
|
if (meta.Parent == p) return true; |
|
} |
|
|
|
return false; |
|
} |
|
|
|
private static void SubscribePivot(Pivot p) |
|
{ |
|
if (managedPivots.Contains(p) == false) |
|
{ |
|
managedPivots.Add(p); |
|
p.Unloaded += OnManagedPivotUnloaded; |
|
} |
|
} |
|
|
|
private static void UnsubscribePivot(Pivot p) |
|
{ |
|
if (managedPivots.Contains(p)) |
|
{ |
|
managedPivots.Remove(p); |
|
p.Unloaded -= OnManagedPivotUnloaded; |
|
|
|
List<PivotItem> keysToRemove = new List<PivotItem>(); |
|
foreach (var pair in disabledItems) |
|
{ |
|
if (pair.Value.Parent == p) keysToRemove.Add(pair.Key); |
|
} |
|
foreach (var key in keysToRemove) disabledItems.Remove(key); |
|
} |
|
} |
|
} |
|
} |
Commentaires récents