0

我有一个 Pivot,其中包含几个静态 PivotItems 和几个动态生成的 PivotItems 在后面的代码中。我希望能够通过更改 PivotItem 的 Visibility 属性来隐藏 PivotHeaderItems。

这是我的代码:

<Style TargetType="PivotHeaderItem">
    <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}" />
    <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
    <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}" />
    <Setter Property="CharacterSpacing" Value="{ThemeResource PivotHeaderItemCharacterSpacing}" />
    <Setter Property="Background" Value="{ThemeResource SystemChromeLowColor}" />
    <Setter Property="Foreground" Value="{ThemeResource SystemControlForegroundBaseHighBrush}" />
    <Setter Property="Padding" Value="{ThemeResource PivotHeaderItemMargin}" />
    <Setter Property="Height" Value="Auto" />
    <Setter Property="VerticalContentAlignment" Value="Center" />
    <Setter Property="IsTabStop" Value="False" />
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="PivotHeaderItem">
                <Grid x:Name="Grid" CornerRadius="4" Margin="4" Visibility="{TemplateBinding Visibility}" Background="{TemplateBinding Background}">
                    <Grid.Resources>
                        <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter">
                            <Setter Property="FontFamily" Value="Segoe UI"/>
                            <Setter Property="FontWeight" Value="SemiBold"/>
                            <Setter Property="FontSize" Value="15"/>
                            <Setter Property="TextWrapping" Value="Wrap"/>
                            <Setter Property="LineStackingStrategy" Value="MaxHeight"/>
                            <Setter Property="TextLineBounds" Value="Full"/>
                            <Setter Property="OpticalMarginAlignment" Value="TrimSideBearings"/>
                        </Style>
                        <Style x:Key="BodyContentPresenterStyle" TargetType="ContentPresenter" BasedOn="{StaticResource BaseContentPresenterStyle}">
                            <Setter Property="FontFamily" Value="{ThemeResource PivotHeaderItemFontFamily}" />
                            <Setter Property="FontWeight" Value="{ThemeResource PivotHeaderItemThemeFontWeight}"/>
                            <Setter Property="FontSize" Value="{ThemeResource PivotHeaderItemFontSize}"/>
                        </Style>
                    </Grid.Resources>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="auto"/>
                        <RowDefinition Height="auto"/>
                    </Grid.RowDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="SelectionStates">
                            <VisualStateGroup.Transitions>
                                <VisualTransition From="Unselected" To="UnselectedLocked" GeneratedDuration="0:0:1.33" />
                                <VisualTransition From="UnselectedLocked" To="Unselected" GeneratedDuration="0:0:1.33" />
                            </VisualStateGroup.Transitions>
                            <VisualState x:Name="Disabled">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                           Storyboard.TargetProperty="Foreground" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlDisabledBaseMediumLowBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Unselected" />
                            <VisualState x:Name="UnselectedLocked">
                                <Storyboard>
                                    <DoubleAnimation Storyboard.TargetName="ContentPresenterTranslateTransform"
                                             Storyboard.TargetProperty="X"
                                             Duration="0" To="{ThemeResource PivotHeaderItemLockedTranslation}" />
                                    <DoubleAnimation Storyboard.TargetName="ContentPresenter"
                                             Storyboard.TargetProperty="(UIElement.Opacity)"
                                             Duration="0" To="0" />
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Selected">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                           Storyboard.TargetProperty="Foreground" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemAccentColorLight3}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemAccentColor}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="UnselectedPointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                           Storyboard.TargetProperty="Foreground" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemControlHighlightAccent3RevealBackgroundBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedPointerOver">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                            Storyboard.TargetProperty="Foreground" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemAccentColorLight3}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Border"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemAccentColor}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="UnselectedPressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                           Storyboard.TargetProperty="Foreground" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemAccentColorLight3}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="SelectedPressed">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                           Storyboard.TargetProperty="Foreground" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseHighBrush}" />
                                    </ObjectAnimationUsingKeyFrames>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                           Storyboard.TargetProperty="Background" >
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SystemAccentColorLight3}" />
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentPresenter
                x:Name="ContentPresenter"
                Content="{TemplateBinding Content}"
                ContentTemplate="{TemplateBinding ContentTemplate}"
                Padding="4 8 4 4"
                Margin="{TemplateBinding Padding}"
                FontSize="{TemplateBinding FontSize}"
                FontFamily="{TemplateBinding FontFamily}"
                FontWeight="{TemplateBinding FontWeight}"
                HorizontalAlignment="{TemplateBinding HorizontalContentAlignment}"
                VerticalAlignment="{TemplateBinding VerticalContentAlignment}"
                        Grid.Row="0">
                        <ContentPresenter.RenderTransform>
                            <TranslateTransform x:Name="ContentPresenterTranslateTransform" />
                        </ContentPresenter.RenderTransform>
                    </ContentPresenter>
                    <Border x:Name="Border" CornerRadius="0 0 4 4" Background="Transparent" Grid.Row="1" Height="4"/>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

这是我的 Pivot XAML 代码:

<Pivot Name="SettingsPivot">

    <PivotItem Name="SubscriptionsandUpdatesPivot" Header="Subscription and Version" Visibility="Collapsed">
        <StackPanel Width="auto" HorizontalAlignment="Stretch">
            <StackPanel Orientation="Horizontal" HorizontalAlignment="Stretch" VerticalAlignment="Top">
                <TextBlock Margin="10,10,10,10" Text="Premium Subscription:" />
                <TextBlock x:Name="SubscriptionStatus" FontWeight="Bold" Margin="8" Text="Loading..." Custom:DockPanel.Dock="Left"/>
            </StackPanel>
            <Button x:Name="BuyMonthlySubscriptionButton" Margin="8" Content="Purchase Monthly Subscription" IsEnabled="False" HorizontalAlignment="Left" Custom:DockPanel.Dock="Right" Click="BuyMonthlySubscriptionButton_Click"/>
            <TextBlock Margin="8" HorizontalAlignment="Left" Visibility="Collapsed" Text=" * unlocks Dashboards, Patients Finder, Sending Emails and Scheduler." />
            <TextBlock Margin="8" HorizontalAlignment="Left" Text=" * in this version, all features are unlocked for testing purposes." />
            <NavigationViewItemSeparator/>
            <StackPanel HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Orientation="Vertical">
                <StackPanel Orientation="Horizontal">
                    <TextBlock Margin="8" Text="Current Version: " />
                    <TextBlock x:Name="VersionStatus" FontWeight="Bold" Margin="8" Text="Please Wait ..." />
                    <HyperlinkButton Name="VersionInfoPage" Content="See Version Features" HorizontalAlignment="Center" />
                </StackPanel>
                <Button x:Name="UpdateButton" Margin="8" Content="Update" HorizontalAlignment="Left" IsEnabled="false"  Click="UpdateButton_Click"/>
                <NavigationViewItemSeparator/>
                <Custom:DockPanel VerticalAlignment="Stretch" HorizontalAlignment="Stretch" Visibility="Collapsed">
                    <ScrollViewer BorderBrush="Gray" BorderThickness="2" CornerRadius="2">
                        
                    </ScrollViewer>
                </Custom:DockPanel>
            </StackPanel>
        </StackPanel>
    </PivotItem>

</Pivot>

我尝试在主网格中设置Visibility="{TemplateBinding Visibility}"以生成 PivotItemHeader 但它不遵循 PivotItem's.Visibility。

这就是现在的设计在此处输入图像描述

4

1 回答 1

0

您不能以PivotItem这种方式更改 a 的可见性,因为“项目”仍保留在控件的项目集合中。相反,您需要将其从控件本身中删除(稍后根据需要重新添加):

// Remove item
SettingsPivot.Items.Remove(SubscriptionsandUpdatesPivot);

// Re-add item
SettingsPivot.Items.Add(SubscriptionsandUpdatesPivot);
于 2021-01-01T10:09:32.723 回答