如何在 UWP 中的 Pivot 中使所选枢轴的标题变为粗体?

Posted

技术标签:

【中文标题】如何在 UWP 中的 Pivot 中使所选枢轴的标题变为粗体?【英文标题】:How to make header of selected pivot bold in a Pivot in UWP? 【发布时间】:2016-10-03 13:50:57 【问题描述】:

我有一个带有标题模板的数据透视表,在这个数据透视表中。 这里的条件是我只想在选择枢轴时才将文本块设为粗体。 我尝试编辑 Pivot 样式但没有,因为我找不到要更改的内容。

<Pivot x:Name="mainContentPivot"  Grid.Row="1"
       ItemContainerStyle="StaticResource CategoriesPivotItemsStyle"
       >
    <Pivot.HeaderTemplate>
        <DataTemplate>
            <StackPanel>
                <TextBlock Text="Binding TitleHeaders" FontSize="16" />
            </StackPanel>
        </DataTemplate>
    </Pivot.HeaderTemplate>
</Pivot>

关于如何实现这一点的任何线索, 谢谢。

【问题讨论】:

您只需要编辑 style template 并将情节提要添加到 VisualStateManager 中的 Selected 状态即可更改 ContentPresenter 的 FontWeight。 【参考方案1】:

您需要执行以下操作:

    %ProgramFiles(x86)%\Windows Kits\10\DesignTime\CommonConfiguration\Neutral\UAP\10.0.10240.0\Generic\generic.xaml 获取PivotHeaderItem 模板

    用它来设计你的PivotHeaderItem

    编辑Selected VisualState 以添加此Setter

    &lt;Setter Target="ContentPresenter.FontWeight" Value="Bold" /&gt;

如果您需要任何帮助,请回复我。

代码在这里:

<Pivot>
...
...
    <Pivot.Resources>
        <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="Transparent" />
            <Setter Property="Foreground" Value="ThemeResource SystemControlForegroundBaseMediumBrush" />
            <Setter Property="Padding" Value="ThemeResource PivotHeaderItemMargin" />
            <Setter Property="Height" Value="48" />
            <Setter Property="VerticalContentAlignment" Value="Center" />
            <Setter Property="IsTabStop" Value="False" />
            <Setter Property="Template">
                <Setter.Value>
                    <ControlTemplate TargetType="PivotHeaderItem">
                        <Grid
                        x:Name="Grid"
                        Background="TemplateBinding Background">
                                <Grid.Resources>
                                    <Style x:Key="BaseContentPresenterStyle" TargetType="ContentPresenter">
                                        <Setter Property="FontFamily" Value="XamlAutoFontFamily"/>
                                        <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>
                                <VisualStateManager.VisualStateGroups>
                                    <VisualStateGroup x:Name="SelectionStates">
                                        <VisualStateGroup.Transitions>
                                            <VisualTransition From="Unselected" To="UnselectedLocked" GeneratedDuration="0:0:0.33" />
                                            <VisualTransition From="UnselectedLocked" To="Unselected" GeneratedDuration="0:0:0.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="ThemeResource SystemControlHighlightTransparentBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
    <!-- Here is where it goes -->          <VisualState.Setters>
                                                <Setter Target="ContentPresenter.FontWeight" Value="Bold" />
                                            </VisualState.Setters>
                                        </VisualState>
                                        <VisualState x:Name="UnselectedPointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightAltBaseMediumHighBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                                   Storyboard.TargetProperty="Background" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightTransparentBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="SelectedPointerOver">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                    Storyboard.TargetProperty="Foreground" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightAltBaseMediumHighBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                                   Storyboard.TargetProperty="Background" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightTransparentBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="UnselectedPressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightAltBaseMediumHighBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                                   Storyboard.TargetProperty="Background" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightTransparentBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                        <VisualState x:Name="SelectedPressed">
                                            <Storyboard>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter"
                                                                   Storyboard.TargetProperty="Foreground" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightAltBaseMediumHighBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                                <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid"
                                                                   Storyboard.TargetProperty="Background" >
                                                    <DiscreteObjectKeyFrame KeyTime="0" Value="ThemeResource SystemControlHighlightTransparentBrush" />
                                                </ObjectAnimationUsingKeyFrames>
                                            </Storyboard>
                                        </VisualState>
                                    </VisualStateGroup>
                                </VisualStateManager.VisualStateGroups>
                                <ContentPresenter
                        x:Name="ContentPresenter"
                        Content="TemplateBinding Content"
                        ContentTemplate="TemplateBinding ContentTemplate"
                        Margin="TemplateBinding Padding"
                        FontSize="TemplateBinding FontSize"
                        FontFamily="TemplateBinding FontFamily"
                        FontWeight="TemplateBinding FontWeight"
                        HorizontalAlignment="TemplateBinding HorizontalContentAlignment"
                        VerticalAlignment="TemplateBinding VerticalContentAlignment">
                                    <ContentPresenter.RenderTransform>
                                        <TranslateTransform x:Name="ContentPresenterTranslateTransform" />
                                    </ContentPresenter.RenderTransform>
                                </ContentPresenter>
                            </Grid>
                        </ControlTemplate>
                    </Setter.Value>
                </Setter>
            </Style>
        </Pivot.Resources>
    </Pivot>

【讨论】:

认真的吗?我需要做所有这些只是为了让它变得大胆?

以上是关于如何在 UWP 中的 Pivot 中使所选枢轴的标题变为粗体?的主要内容,如果未能解决你的问题,请参考以下文章

Swift - 如何更改SCNNode对象的Pivot

在 h2 中创建枢轴

在 Pivot 上对数组进行分区

如何使快速排序递归?

Windows 10 UWP:无法停止枢轴控制循环

如何在 UWP 中使 PathIcon 拉伸