How to Hide Visibility of Individual PivotItem
Asked Answered
B

1

11

I have a few pivot items in my page, and based upon whether the app is in trial mode or not I need to show or hide one of the PivotItems. Setting the Visibility of the PivotItem directly in XAML or in C# only hides what is within the PivotItem, not the actual PivotItem itself. How can I accomplish this?

In testing I've tried both of the following

Page.xaml

<phone:PivotItem x:Name="PivotItem2" Visibility="Collapsed"
                         Header="2">
                ...
</<phone:PivotItem>

OR

Page.xaml.cs

 protected override void OnNavigatedTo(NavigationEventArgs e)
    {
        base.OnNavigatedTo(e);

        //Check trial state and set PivotItem
        if ((Application.Current as App).IsTrial)
        {
            PivotItem2.Visibility = Visibility.Collapsed;
        }
        else
        {
            PivotItem2.Visibility = Visibility.Visible;
        }
    }
Bathe answered 13/11, 2014 at 12:36 Comment(3)
Have you tried deleting the PivotItem?Trilby
I still need to show it when not in a trial state.Bathe
Maybe you can do it such a way: Prepare the whole pivot, in Page's constructor check if trial mode - if yes - remove the pivotitem.Trilby
J
15

You can only remove or add PivotItems dynamically in your Pivot using Pivot.Items collection. You can not hide them. As per your requirement, you can do this :

//Check trial state and set PivotItem
if ((Application.Current as App).IsTrial)
{
    PivotControl.Items.Remove(PivotControl.Items.Single(p => ((PivotItem)p).Name == "Name_PivotItem"));
}
else
{
    PivotControl.Items.Add(PivotControl.Items.Single(p => ((PivotItem)p).Name == "Name_PivotItem"));
}
Jodhpurs answered 13/11, 2014 at 13:56 Comment(2)
What if I need to add this as the second pivot item in the Pivot control, index 1.Bathe
You need to work around 'index' property of PivotItem.Jodhpurs

© 2022 - 2024 — McMap. All rights reserved.