微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

如何在 UWP Visibility 中制作 PivotItemHeader 跟随 PivotItem 的可见性?

如何解决如何在 UWP Visibility 中制作 PivotItemHeader 跟随 PivotItem 的可见性?

我有一个 Pivot,其中包含几个静态 PivotItems 和几个在代码后面动态生成的 Pivot。我希望能够通过更改 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" 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。

这就是设计现在的样子

enter image description here

解决方法

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

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

// Re-add item
SettingsPivot.Items.Add(SubscriptionsandUpdatesPivot);

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。