如何隐藏单个PivotItem的可见性

11

我的页面中有几个枢轴项,根据应用程序是否处于试用模式,我需要显示或隐藏其中一个 PivotItem。在 XAML 或 C# 中直接设置 PivotItem 的可见性只会隐藏 PivotItem 内部的内容,而不是 PivotItem 本身。我该如何实现这一点?

在测试中,我尝试了以下两种方法:

Page.xaml

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

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;
        }
    }

1
你尝试过删除 PivotItem 吗? - Romasz
我仍然需要在非试用状态下展示它。 - Matthew
也许你可以这样做:准备整个数据透视表,在页面的构造函数中检查试用模式-如果是-则删除数据透视表项。 - Romasz
1个回答

15
您只能使用 Pivot.Items 集合在数据透视表中动态地添加或删除 PivotItems。无法将它们隐藏起来。根据您的需求,可以执行以下操作:
//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"));
}

如果我需要将其作为 Pivot 控件的第二个枢轴项添加,即索引为 1,该怎么办? - Matthew
你需要围绕 PivotItem 的 'index' 属性进行工作。 - user3497034

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接