使用VisualStateManager Windows 10更改Pivot PivotHeaderItem FontSize

时间:2016-07-25 07:19:03

标签: xaml windows-10 windows-10-universal visualstatemanager

我有一个Pivot,其格式如下

  <Page.Resources>

    <Style TargetType="PivotHeaderItem"  x:Name="PivotHeaderItem800">
        <Setter Property="Foreground" Value="Gray"/>
        <Setter Property="FontFamily" Value="ms-appx:///Assets/Fonts/garfield the cat.ttf#garfield the cat"/>
        <Setter Property="FontSize" Value="40" />
        <Setter Property="Template">
            <Setter.Value>
                <ControlTemplate TargetType="PivotHeaderItem">
                    <Grid x:Name="Grid" Background="{TemplateBinding Background}">
                        <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="White" />
                                            <!-- original value {ThemeResource SystemControlHighlightAltBaseHighBrush} -->
                                        </ObjectAnimationUsingKeyFrames>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="Grid" Storyboard.TargetProperty="Background">
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightTransparentBrush}" />
                                        </ObjectAnimationUsingKeyFrames>
                                    </Storyboard>
                                </VisualState>
                                <VisualState x:Name="UnselectedPointerOver">
                                    <Storyboard>
                                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="ContentPresenter" Storyboard.TargetProperty="Foreground">
                                            <!--<DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource SystemControlHighlightAltBaseMediumHighBrush}" />-->
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="Green" />
                                        </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}" />-->
                                            <DiscreteObjectKeyFrame KeyTime="0" Value="White" />
                                        </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>
</Page.Resources>

使用VisualStateManager我想在FontSize为真时将AdaptiveTrigger MinWindowWidth="500"缩减为24。如何正确地在Page.Resource中创建新样式?我想要像

这样的东西
    <Style TargetType="PivotHeaderItem"  x:Name="PivotHeaderItem500" BasedOn="{StaticResource PivotHeaderItem800}">
        <Setter Property="FontSize" Value="24" />
    </Style>

Visual Studio抱怨The key is already defined in this scope.

VisualStateManager目前有以下内容。创建新样式后,我想将其应用于VisualStateMin500

 <VisualStateManager.VisualStateGroups>
        <VisualStateGroup x:Name="VisualStateGroup">
            <VisualState x:Name="VisualStateMin800">
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="800"/>
                </VisualState.StateTriggers>
            </VisualState>
            <VisualState x:Name="VisualState500">
                <VisualState.Setters>
                    <Setter Target="textBlockTitle.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>20,36,20,24</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="textBlockTitle.(TextBlock.FontSize)" Value="48"/>
                    <Setter Target="rootPivot.(FrameworkElement.Width)" Value="500"/>
                    <Setter Target="listView0.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>18</Thickness>
                        </Setter.Value>
                    </Setter>

                    <Setter Target="listView1.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>18</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="listView2.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>18</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="listView3.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>18</Thickness>
                        </Setter.Value>
                    </Setter>
                    <Setter Target="listView4.(FrameworkElement.Margin)">
                        <Setter.Value>
                            <Thickness>18</Thickness>
                        </Setter.Value>
                    </Setter>
                </VisualState.Setters>
                <VisualState.StateTriggers>
                    <AdaptiveTrigger MinWindowWidth="500"/>
                </VisualState.StateTriggers>
            </VisualState>
        </VisualStateGroup>
    </VisualStateManager.VisualStateGroups>

我的Pivot xaml如下

  <RelativePanel>
        <control:ProgressRingWithText RelativePanel.AlignHorizontalCenterWithPanel="True" RelativePanel.AlignVerticalCenterWithPanel="True"/>
        <TextBlock x:Name="textBlockTitle" Text="Leaderboards" Style="{StaticResource HeaderTextBlockStyle}" RelativePanel.AlignHorizontalCenterWithPanel="True" Margin="24,48,24,28" FontSize="64"/>

        <Pivot x:Name="rootPivot" RelativePanel.Below="textBlockTitle" RelativePanel.AlignHorizontalCenterWithPanel="True" Width="800" Style="{StaticResource PivotStyle1}">
            <Interactivity:Interaction.Behaviors>
                <Core:EventTriggerBehavior EventName="SelectionChanged">
                    <Core:InvokeCommandAction Command="{Binding SelectionChangedCommand}"
                                          InputConverter="{StaticResource SelectionChangedEventArgsConverter}"
                                          InputConverterParameter="{Binding ElementName=rootPivot, Path=SelectedIndex}"/>
                </Core:EventTriggerBehavior>
            </Interactivity:Interaction.Behaviors>
            <PivotItem x:Name="pivotItem0" Header="{Binding LevelVeryEasyName}" >
                <ListView x:Name="listView0" ItemsSource="{Binding ScoreList}" IsItemClickEnabled="False" Margin="24">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <control:PivotListViewItemTemplate />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </PivotItem>

            <PivotItem  x:Name="pivotItem1" Header="{Binding LevelEasyName}">
                <ListView x:Name="listView1" ItemsSource="{Binding ScoreList}" IsItemClickEnabled="False" Margin="24">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <control:PivotListViewItemTemplate />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </PivotItem>
            <PivotItem x:Name="pivotItem2" Header="{Binding LevelMediumName}">
                <ListView x:Name="listView2" ItemsSource="{Binding ScoreList}" IsItemClickEnabled="False" Margin="24">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <control:PivotListViewItemTemplate />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </PivotItem>
            <PivotItem x:Name="pivotItem3" Header="{Binding LevelHardName}">
                <ListView x:Name="listView3" ItemsSource="{Binding ScoreList}" IsItemClickEnabled="False" Margin="24">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <control:PivotListViewItemTemplate />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </PivotItem>
            <PivotItem x:Name="pivotItem4" Header="{Binding LevelInsaneName}">
                <ListView x:Name="listView4" ItemsSource="{Binding ScoreList}" IsItemClickEnabled="False" Margin="24">
                    <ListView.ItemContainerStyle>
                        <Style TargetType="ListViewItem">
                            <Setter Property="HorizontalContentAlignment" Value="Stretch" />
                        </Style>
                    </ListView.ItemContainerStyle>
                    <ListView.ItemTemplate>
                        <DataTemplate>
                            <control:PivotListViewItemTemplate />
                        </DataTemplate>
                    </ListView.ItemTemplate>
                </ListView>
            </PivotItem>
        </Pivot>
    </RelativePanel>

1 个答案:

答案 0 :(得分:0)

而不是直接在Header中绑定它。将其改为以下。

<PivotItem.Header>
     <TextBlock Text="{Binding LevelVeryEasyName}" x:Name="PivotItem0"/>
</PivotItem.Header>

这样您就可以完全访问Pivot Header。

现在你的二传手将是

<Setter Target="PivotItem0.(TextBlock.FontSize)" Value="24"/>

看看它是否有效。让我知道结果。