Windows Phone 8.1 Pivot自定义标题样式

时间:2014-08-15 13:43:25

标签: c# xaml windows-phone-8.1

我的目的是模仿这里看到的类似效果:http://www.visuallylocated.com/post/2012/05/23/Changing-the-background-color-of-your-pivot-headers.aspx。 网上有资源描述如何操作,但所有资源都适用于Windows Phone 8. 8.1更新带来了严重的API更改,使代码无用。

那么,我如何设置pivot头的样式呢?我找到了名为Windows.UI.Xaml.Controls.Primitives的名称空间,其中包含类PivotHeaderPanel,在这种情况下可能会有所帮助,但我无法找到从XAML访问此类的方法。 或许还有另一种方式?

2 个答案:

答案 0 :(得分:15)

如果您只想更改所有标题的背景颜色,可以在Window Phone 8.1中执行此操作。

首先,使用Expression Blend生成Pivot控件的默认样式。

<Thickness x:Key="PivotPortraitThemePadding">19,38,0,0</Thickness>
<Thickness x:Key="PivotLandscapeThemePadding">19,25,0,0</Thickness>
<Style x:Key="CustomPivotStyle" TargetType="Pivot">
    <Setter Property="Margin" Value="0"/>
    <Setter Property="Padding" Value="0"/>
    <Setter Property="Foreground" Value="{ThemeResource PhoneForegroundBrush}"/>
    <Setter Property="Background" Value="Transparent"/>
    <Setter Property="ItemsPanel">
        <Setter.Value>
            <ItemsPanelTemplate>
                <Grid/>
            </ItemsPanelTemplate>
        </Setter.Value>
    </Setter>
    <Setter Property="Template">
        <Setter.Value>
            <ControlTemplate TargetType="Pivot">
                <Grid x:Name="RootElement" Background="{TemplateBinding Background}" HorizontalAlignment="{TemplateBinding HorizontalAlignment}" VerticalAlignment="{TemplateBinding VerticalAlignment}">
                    <Grid.RowDefinitions>
                        <RowDefinition Height="Auto"/>
                        <RowDefinition Height="*"/>
                    </Grid.RowDefinitions>
                    <VisualStateManager.VisualStateGroups>
                        <VisualStateGroup x:Name="Orientation">
                            <VisualState x:Name="Portrait">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="TitleContentControl">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotPortraitThemePadding}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                            <VisualState x:Name="Landscape">
                                <Storyboard>
                                    <ObjectAnimationUsingKeyFrames Storyboard.TargetProperty="Margin" Storyboard.TargetName="TitleContentControl">
                                        <DiscreteObjectKeyFrame KeyTime="0" Value="{ThemeResource PivotLandscapeThemePadding}"/>
                                    </ObjectAnimationUsingKeyFrames>
                                </Storyboard>
                            </VisualState>
                        </VisualStateGroup>
                    </VisualStateManager.VisualStateGroups>
                    <ContentControl x:Name="TitleContentControl" ContentTemplate="{TemplateBinding TitleTemplate}" Content="{TemplateBinding Title}" Style="{StaticResource PivotTitleContentControlStyle}"/>
                    <ScrollViewer x:Name="ScrollViewer" HorizontalSnapPointsAlignment="Center" HorizontalSnapPointsType="MandatorySingle" HorizontalScrollBarVisibility="Hidden" Margin="{TemplateBinding Padding}" Grid.Row="1" Template="{StaticResource ScrollViewerScrollBarlessTemplate}" VerticalSnapPointsType="None" VerticalScrollBarVisibility="Disabled" VerticalScrollMode="Disabled" VerticalContentAlignment="Stretch" ZoomMode="Disabled">
                        <PivotPanel x:Name="Panel" VerticalAlignment="Stretch">
                            <PivotHeaderPanel x:Name="Header" Background="{TemplateBinding BorderBrush}">
                                <PivotHeaderPanel.RenderTransform>
                                    <CompositeTransform x:Name="HeaderTranslateTransform" TranslateX="0"/>
                                </PivotHeaderPanel.RenderTransform>
                            </PivotHeaderPanel>
                            <ItemsPresenter x:Name="PivotItemPresenter">
                                <ItemsPresenter.RenderTransform>
                                    <TranslateTransform x:Name="ItemsPresenterTranslateTransform" X="0"/>
                                </ItemsPresenter.RenderTransform>
                            </ItemsPresenter>
                        </PivotPanel>
                    </ScrollViewer>
                </Grid>
            </ControlTemplate>
        </Setter.Value>
    </Setter>
</Style>

在下面的这一行中,我对默​​认样式所做的唯一更改是将Background="{TemplateBinding BorderBrush}"添加到PivotHeaderPanel,这是承载所有标题的面板。

<PivotHeaderPanel x:Name="Header" Background="{TemplateBinding BorderBrush}">

我在这里使用TemplateBinding的原因是,这样做可以让我灵活地通过指定BorderBush的{​​{1}}来更改标题背景。由于Pivot未在控件中的任何位置使用,因此如果我们更改它将不会有任何副作用。

所以,你需要在BorderBrush中做的就是这个。

Pivot

这就是他们现在的样子。

enter image description here

希望这有帮助!

答案 1 :(得分:2)

所以8.1杀了我以前做HeaderTemplates的方式。

我现在的解决方案是在PivotItem的Header元素中放置一个自定义的TextBlock或Control。


<Pivot>

    <PivotItem>
        <PivotItem.Header>
            <Grid Background="Red">
                <TextBlock Text="WHY DID YOU DO THIS MICROSOFT"/>
            </Grid>
        </PivotItem.Header>
    </PivotItem>
    ...
</Pivot>
相关问题