如何隐藏PivotItem?

Jim*_*röm 15 silverlight xaml pivot windows-phone-7

我有一个带Pivot控件的页面,在某些情况下我不想显示特定的内容PivotItem.
设置Visibility为折叠似乎根本不会影响它.

有什么建议?

Ste*_*SFT 14

您应该能够通过在Pivot.Items上使用相应的集合方法在您的Pivot中动态删除或添加PivotItems.

如果这对您的方案不起作用,请告诉我.

  • 非常感谢我使用了以下代码:NameOfPivotControl.Items.Remove(NameOfPivotControl.Items.Single(p =>((PivotItem)p).Name =="NameOfPivotItem")); (2认同)

Baj*_*ena 10

我已经创建了一个自定义行为来显示/隐藏枢轴项

用法:

< i:Interaction.Behaviors>
    < common:HideablePivotItemBehavior Visible="{Binding variable}" />
</ i:Interaction.Behaviors >
Run Code Online (Sandbox Code Playgroud)

码:

/// <summary>
/// Behavior which enables showing/hiding of a pivot item`
/// </summary>
public class HideablePivotItemBehavior : Behavior<PivotItem>
{
    #region Static Fields

    public static readonly DependencyProperty VisibleProperty = DependencyProperty.Register(
        "Visible",
        typeof(bool),
        typeof(HideablePivotItemBehavior),
        new PropertyMetadata(true, VisiblePropertyChanged));

    #endregion

    #region Fields

    private Pivot _parentPivot;

    private PivotItem _pivotItem;

    private int _previousPivotItemIndex;

    private int _lastPivotItemsCount;

    #endregion

    #region Public Properties

    public bool Visible
    {
        get
        {
            return (bool)this.GetValue(VisibleProperty);
        }

        set
        {
            this.SetValue(VisibleProperty, value);
        }
    }

    #endregion

    #region Methods

    protected override void OnAttached()
    {
        base.OnAttached();

        this._pivotItem = AssociatedObject;
    }

    private static void VisiblePropertyChanged(DependencyObject dpObj, DependencyPropertyChangedEventArgs change)
    {
        if (change.NewValue.GetType() != typeof(bool) || dpObj.GetType() != typeof(HideablePivotItemBehavior))
        {
            return;
        }

        var behavior = (HideablePivotItemBehavior)dpObj;
        var pivotItem = behavior._pivotItem;

        // Parent pivot has to be assigned after the visual tree is initialized
        if (behavior._parentPivot == null)
        {
            behavior._parentPivot = (Pivot)behavior._pivotItem.Parent;
            // if the parent is null return
            if (behavior._parentPivot == null)
            {
                return;
            }
        }

        var parentPivot = behavior._parentPivot;
        if (!(bool)change.NewValue)
        {
            if (parentPivot.Items.Contains(behavior._pivotItem))
            {
                behavior._previousPivotItemIndex = parentPivot.Items.IndexOf(pivotItem);
                parentPivot.Items.Remove(pivotItem);
                behavior._lastPivotItemsCount = parentPivot.Items.Count;
            }
        }
        else
        {
            if (!parentPivot.Items.Contains(pivotItem))
            {
                if (behavior._lastPivotItemsCount >= parentPivot.Items.Count)
                {

                    parentPivot.Items.Insert(behavior._previousPivotItemIndex, pivotItem);
                }
                else
                {
                    parentPivot.Items.Add(pivotItem);
                }
            }
        }
    }
    #endregion
}
Run Code Online (Sandbox Code Playgroud)

  • 非常好!这是唯一适合我的解决方案(mvvm)! (2认同)

小智 7

您可以从父透视控件中删除透视项

parentPivotControl.Items.Remove(pivotItemToBeRemoved);
Run Code Online (Sandbox Code Playgroud)